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
andjobs
is many for many and is made using the helper tablejob_user
. - A
job
belongs toproject
.
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?