首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >雄辩中从句的Laravel分组

雄辩中从句的Laravel分组
EN

Stack Overflow用户
提问于 2017-09-15 11:05:49
回答 2查看 415关注 0票数 0

我在laravel 5.4中创建了雄辩的查询,其中我想通过6个不同的组合过滤数据,具体如下:

  1. 类别
  2. 子范畴
  3. 产业
  4. 风格
  5. 方向
  6. 颜色

这是我使用过的查询

代码语言:javascript
复制
 $updatedproducts  = Product::Where('category', $cat)
                     ->Where('subcategory', $subcat)
                     ->whereIn('industry', $industrytags)
                     ->orWhereIn('style', $styletags)
                     ->orWhereIn('orientation', $orientationtags)
                     ->orWhereIn('color', $colortags)
                     ->paginate(12);

它的工作很好,除了当我从不同类别和子类别中获取结果时忽略了类别和子类别的一件事,我想要数据,其中类别和子类别应该总是匹配的,其他4个剩余的过滤器可以是可选的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-15 11:14:03

您需要分组为可选条件:

代码语言:javascript
复制
$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);
票数 1
EN

Stack Overflow用户

发布于 2017-09-15 11:12:26

以下列方式尝试您的查询:

代码语言:javascript
复制
$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匹配,其他字段是可选的。

希望这能帮上忙

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46238106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档