Event::with(['teams' => function ($query) use ($user) {
// ...
}])
Jul 5, 2023
7
Level 63
How to add condition on a relationship if the relationship is present ?
Hello,
I need to add a condition on a relationship if the relationship is present. How is it possible to do that ?
Example
$events = Event::
whereHas('teams', function ($query) use ($user) {
// ...
})
->get();
But this will filter the events which have teams. I need the events with and without teams, but for those which have teams, only retrieve the events if a certain condition is satisfied on the relationship between a team and a user (team_user table).
How is it possible to do that ?
Thanks for your help.
V
Level 14
$events = Event::query()
->whereHas('teams', function ($query) use ($user) {
// ...
})
->orWhereDoesntHave('teams')
->get();
1 like
Please or to participate in this conversation.