When some queries become too complex, you can try to use a raw query.
Aug 31, 2022
2
Level 1
I want to do a when case in an2 Eloquent query.
Hi everyone. I have used the when() for checking if some variables exists, but I want to know if I can use the when() to check if a property of a column of the relation is one thing or another. Something like that:
$dogshows = Dogshow::with('country')->with('club.breeds')->with('thumb')->with('card')
->with('shows')
->where('active', 1)
->whereHas('club', function($q) {
$q->when('club_type == sb', function($query) {
$query->whereHas('breeds', function($que) {
$que->whereIn('breeds.id', auth()->user()->breed_favorites->pluck('favable_id')->toArray());
});
})
->when('club_type == ab', function($query) {
$query->whereHas('system.breeds.breeds', function ($query) {
$query->whereIn('breeds.id', auth()->user()->breed_favorites->pluck('favable_id')->toArray());
});
});
})->get();
Please or to participate in this conversation.