I’m trying to only return tiers that have options associated to them. My query has a has()
method, and a query on my options, but for some reason adding ->has('tiers.options', '>', 0)
still returns options that has an empty array. What am I missing here?
$buyers = Buyer::with([
'tiers.options' => function ($query) {
$query->where('type', 'private');
}
])->has('tiers.options', '>', 0)->get();
2
Answers
Have you tried to use whereHas() instead?
https://laravel.com/docs/10.x/eloquent-relationships#querying-relationship-existence
It would be something like that if I understand correctly:
how about adding whereHas quary inside of ‘with’
eg:
here ‘tiers’ that only have options with type private are will fetched.