This indeed limits the jobs to only jobs that contain teams whose team_member_quota > 5
$query = Eloquent\Models\Job::whereHas('phases', function($query)
{
$query->whereHas('teams', function ($q)
{
$q->whereRaw("teams.team_member_quota > 5");
});
});
However, adding the "with" clause
$query->with('phases.teams');
still brings back all the phases for each of those jobs, whether, they contain teams that meet the criteria or not, and all the teams within those phases, whether they have a team_member_quota > 5 or not.
I found this "with" clause worked
$query->with(array(
"phases" => function($q1)
{
$q1->whereHas('teams', function($q2)
{
$q2->whereRaw('teams.team_member_quota > 5');
});
},
"phases.teams" => function($q)
{
$q->whereRaw("teams.team_member_quota > 5");
}
));
providing only the phases that contain teams that meet the criteria, and teams that meet the criteria within the results.