Reputation: 133
I am building my app and I needed to use leftJoin
in laravel
I prepared a query in database and it works fine.
SELECT sp.id, ssp.sale_id, ssp.product_id FROM store_products sp
LEFT JOIN store_sale_products ssp ON sp.id = ssp.product_id
WHERE ssp.sale_id IS NULL AND sp.store_id = 1
Now I am implementing this query on laravel and I tried this
$nosaleproducts = Store_product::leftJoin('store_sale_products','store_products.id','=','store_sale_products.product_id')
->where('store_sale_products.sale_id','IS','NULL')
->where('store_products.store_id','=',session::get('store_id'))
->get(['store_products.id','store_products.product_name']);
But this gets me no results. Can anyone specify what is wrong with my query?
Upvotes: 1
Views: 102
Reputation: 2815
Use this
$nosaleproducts = Store_product::leftJoin('store_sale_products',function($join){
$join->on('store_products.id','=','store_sale_products.product_id');})
->whereNull('store_sale_products.sale_id')
->where('store_products.store_id','=',session::get('store_id'))
->get(['store_products.id','store_products.product_name']);
Upvotes: 1