@alex_hill Something like this although it's untested should point you the right way
return User::whereId($user_id)->whereHas('jobs.stages' , function($query) {
$query->where('status', 'ready')->orderBy('order');
})->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am having a bit of trouble with Eloquent.
I have three models:
public function jobs()
{
return $this->hasMany('App\Job')->withTimestamps();
}
protected $table = 'jobs';
protected $fillable = [
'address',
'user_id',
];
public function users()
{
return $this->belongsTo('App\User')->withTimestamps();
}
public function stages()
{
return $this->belongsToMany('App\Stage')->withTimestamps()->withPivot('status','order');
}
protected $table = 'stages';
protected $fillable = [
'title',
'description',
];
public function faqs()
{
return $this->belongsToMany('App\Faq')->withTimestamps();
}
public function jobs()
{
return $this->belongsToMany('App\Job')->withPivot('status','order');
}
On the job_stage pivot table I have two extra columns, order and status. I would like to find all stages belonging to a user, where the status is 'ready' and order by the 'order' column. As a newbie I am struggling with the three table join and filtering based on the pivot table fields - any pointers would be appreciated.
Thanks,
Please or to participate in this conversation.