Saw some other posts about and found that it is possible to use the Laravel Eloquent leftJoin()
to do queries with multiples 'on' clauses. I was wondering (and didn't find anything into the laravel docs) if it's possible to build an eager loading eloquent query builder with multiple 'on' clauses on it. An example would be transform:
select * from tablea ta left join tableb tb on tablea.id = tableb.table_a_id and tableb.foo = ? left join tablec tc on tableb.id = tablec.table_b_id group by ta.name
into something like:
TableA::with([
'tableb'=> fn($q)=>$q->on()->on(),
'tableb.tablec'=> fn($q2)=>$q2->on()
])->get();
Does Laravel has something like this or I have to use the leftJoin('tableb',fn($q)=>$q->on()->on())
??
Yes. Possible .
It should be something like:
$tableA = App\TableA::with(['tableb' => function ($query) {
$query->where('example', 'like', '%test%');
}])->get();
or like following.
$tableA = App\TableA::whereHas('tableb', function ($q) {
$q->where('tableb.date', '<', 'tablea.date'); // or whatever the columns are
})->with('tablec')->get();
Modify as per your relations among tables. For more, Please visit constraining-eager-loads