2017-02-04 36 views
1

我建设我的应用程序,我需要使用leftJoin在laravel左侧的接合部在Laravel

我准备在数据库中的查询,并能正常工作。

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 

现在我实现上laravel此查询和我想这

$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']); 

但是,这让我没有结果。任何人都可以指定我的查询有什么问题吗?

回答

1

使用此

$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']);