jeudi 28 février 2019

How to do in Laravel, subquery where

I've see a lot of examples on how to do subquery with whereIn, but i need to do the same with where, like this:

select *
  from products
 where (select count(*) 
             from items 
           where items.product_id = products.id 
              and items.exported = 0) = 0;

I simply tried this code:

Product::where(function($q) {
    $q->selectRaw('count(*)')
        ->from('items')
        ->whereRaw('items.product_id', 'products.id')
        ->where('items.exported', 0);
}, '=', 0);

In this solution the result query is something like this:

select *
  from products
where (items.product_id and exported = 0);

The builder loose the subquery for some reason. How i can solve ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire