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 ?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2NzMh18
via IFTTT

Aucun commentaire:

Enregistrer un commentaire