问题描述
我的laravel“ where”似乎被忽略了。 -> where('products.hide_product','=','N') 关于我在做什么错的任何想法吗?
$products = Product::join('brands','products.brand_id','=','brands.brand_id')
->join('categories','products.cat_id','categories.cat_id')
->leftJoin('images','products.product_id','images.product_id')->where('img_priority','1')->orWhere('img_priority',null)
->where('products.hide_product','N')->where('products.group_id',$groupID)->orderBy('img_priority','DESC')
->get(array('products.en_71','products.astm','images.file_name','products.product_name','products.collect_part_no','brands.brand_name','categories.cat_name','products.status','images.file_type','products.pop','products.color_label','products.ai_complete'));
解决方法
您可以使用第一个where
那里的连接来移动连接:
$products = Product::join('brands','products.brand_id','=','brands.brand_id')
->join('categories','products.cat_id','categories.cat_id')
->leftJoin('images',function ($join) {
$join->on('products.product_id','images.product_id')
->where('img_priority','1')
->orWhere('img_priority',null);
})
->where(
['products.hide_product','N'],['products.group_id',$groupID],)
->select('products.en_71','products.astm','images.file_name','products.product_id','products.product_name','products.collect_part_no','brands.brand_name','categories.cat_name','products.status','images.file_type','images.img_priority','products.pop','products.color_label','products.ai_complete')
->orderBy('img_priority','DESC')
->get();
,
请尝试以下代码。
$products = Product::join('brands',function($join)) {
$join->on('products.product_id','images.product_id')
->where(function($query){
$query->where('img_priority','1')
->orWhere('img_priority',null)
})
}
->where(function($query) {
$query->where('products.hide_product','N')
->where('products.group_id',$groupID)
})
->orderBy('img_priority','DESC')
->get(array('products.en_71','products.ai_complete')
);