I have this query:
$tournament = Tournament::with(
'championships',
'championships.settings',
'championships.category',
'championships.tree.users',
)->first();
Right now, it gives me the tournament with all attached championships.
What I need is to get the tournament but only with the championship which match the $request->championshipId
that I have.
Off course, I also want this filter in all the other relations ('championships.settings', 'championships.category','championships.tree.users')
You can use Constraining Eager Loading
like this:
$tournaments = Tournament::with(['championships' => function ($query) {
$query->where('something', 'like', '%this%');
},
'championships.settings' => function ($query) {
$query->where('something', 'like', '%this%');
},
...])->get();
Load the relations in the subquery constraint:
$tournament = Tournament::with(['championships' => function($query) use ($request) {
return $query->where('id', $request->championshipId)
->with([
'settings',
'category',
'tree.users'
]);
}])->first();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With