所以这里是我想要的查询
$products = Product::whereHas('ProductFilter' , function($q) use ($filter_groups){
foreach($filter_groups as $k=>$v)
$q->where('f_id' , $k )->whereIn( 'value' ,$v );
})->get();
将导致此查询
select * from `products` where exists
(
select * from `product_filters` where
`product_filters`.`product_id` = `products`.`id` and
`f_id` = '4' and `value` in ('10', '11', '12', '13' ) and
`f_id` = '10' and `value` in ('34')
)
我想将这些行分组为
`f_id` = '4' and `value` in ('10', '11', '12', '13' )
and
`f_id` = '10' and `value` in ('34')
像
( `f_id` = '4' and `value` in ('10', '11', '12', '13' ) )
and
( `f_id` = '10' and `value` in ('34') )
答案 0 :(得分:1)
您可以通过嵌套where()
方法调用对where子句进行分组,这样您的示例将如下所示:
$products = Product::whereHas('ProductFilter' , function($q) use ($filter_groups){
foreach($filter_groups as $k=>$v) {
$q->where(function (Builder $query) {
$query->where('f_id', $k);
$query->whereIn('value' ,$v );
});
})->get();
Here是文档的相关部分。