我在laravel 5.4中创建了雄辩的查询,其中我想通过6个不同的组合过滤数据,具体如下:
这是我使用过的查询
$updatedproducts = Product::Where('category', $cat)
->Where('subcategory', $subcat)
->whereIn('industry', $industrytags)
->orWhereIn('style', $styletags)
->orWhereIn('orientation', $orientationtags)
->orWhereIn('color', $colortags)
->paginate(12);它的工作很好,除了当我从不同类别和子类别中获取结果时忽略了类别和子类别的一件事,我想要数据,其中类别和子类别应该总是匹配的,其他4个剩余的过滤器可以是可选的。
发布于 2017-09-15 11:14:03
您需要分组为可选条件:
$updatedproducts = Product::Where('category', $cat)
->Where('subcategory', $subcat)
->where(function ($where) use ($industrytags, $styletags, $orientationtags, $colortags) {
$where->whereIn('industry', $industrytags)
->orWhereIn('style', $styletags)
->orWhereIn('orientation', $orientationtags)
->orWhereIn('color', $colortags);
})
->paginate(12);发布于 2017-09-15 11:12:26
以下列方式尝试您的查询:
$updatedproducts = Product::Where('category', $cat)
->Where('subcategory', $subcat)
->where(function($q) use($industrytags ,$styletags, $orientationtags,$colortags) {
$q->whereIn('industry', $industrytags)
->orWhereIn('style', $styletags)
->orWhereIn('orientation', $orientationtags)
->orWhereIn('color', $colortags);
})
->paginate(12);它总是与category && subcategory匹配,其他字段是可选的。
希望这能帮上忙
https://stackoverflow.com/questions/46238106
复制相似问题