Relationship at Laravel

0

Talk about it, I'm starting to use laravel and I have a problem when selecting some data.

I have the following tables: users , jobs , projects , job_user .

  • The table relationship of users and jobs is many for many and is made using the helper table job_user .
  • A job belongs to project .

Relationships in the Model Job :

public function scopeAuthUser($q)
{
    return $q->whereHas('users', function($q){
        $q->where('user_id', auth()->user()->id);
    });
}

public function project()
{
    return $this->belongsTo(Project::class);
}

public function users()
{
    return $this->belongsToMany(User::class);
}

Relationships in the Model Project :

public function user()
{
    return $this->belongsTo(User::class);
}

public function jobs()
{
    return $this->hasMany(Job::class);
}

Relationships in the Model User :

public function jobs()
{
    return $this->belongsToMany(Job::class);
}

JobsController:

public function index()
{
    $jobs = $this->job->authUser()->with('project.company')->get();

    return view('jobs.index', compact('jobs'));
}

The question is, how do I in ProjectsController show only Projects that have Jobs that the user is linked to?

    
asked by anonymous 12.06.2018 / 03:39

0 answers