Я видел много примеров того, как сделать подзапрос с whereIn
, но мне нужно сделать то же самое с where
, например:
select * from products
where (select count(*) from items
where items.product_id = products.id
and items.exported = 0) = 0;
Я просто попробовал этот код:
Product::where(function($q) {
$q->selectRaw('count(*)')
->from('items')
->whereRaw('items.product_id', 'products.id')
->where('items.exported', 0);
}, '=', 0);
В этом решении запрос результата выглядит примерно так:
select * from products
where (items.product_id and exported = 0);
По какой-то причине сборщик теряет подзапрос.Как я могу решить?
РЕШЕНИЕ
Product::whereHas('products', function($q) {
$q->selectRaw('count(*)')
->from('items')
->whereRaw('items.product_id', 'products.id')
->where('items.exported', 0);
}, '=', 0);