Laravel雄辩地在哪里和哪个问题获取正确的数据

时间:2017-11-08 18:56:26

标签: php laravel laravel-5 eloquent laravel-eloquent

我有以下查询构建器生成此查询:

当使用最小和最大价格过滤时,我的问题在于销售和库存选项中的where条件。我的网址如下所示:

category/men-clothes?sale=no_sale&min_price=10&max_price=10000&per_page=8

sale = no_sale,表示查询执行where' sale_price',' =',0,因此这在我的SQL查询中:

and `sale_price` = ? order by `id` desc

然而,它仍然提取sales_price大于0的产品,但是当我切换到on_sale时,只提取sales_price大于0的产品。所以问题只在选择没有活跃销售的产品时。 对于on_stock和no_stock来说,它只是没有获取正确的产品。我知道建造者很大,但我还没能弄清楚它为什么不能正常工作。它只有在我完全删除min_price和max_price过滤后才能正常工作所以它必须位于导致这些问题的价格过滤的where和orWhere中。

select * from `products` where exists (select * from `categories` inner join `product_categories` on `categories`.`id` = `product_categories`.`category_id` where `products`.`id` = `product_categories`.`product_id` and `id` = ?) and (`has_variants` = ? and `price` >= ?) or (`has_variants` = ? and `min_price` != ? and `min_price` >= ?) and (`has_variants` = ? and `price` <= ?) or (`has_variants` = ? and `max_price` != ? and `max_price` <= ?) and `sale_price` = ? order by `id` desc  

$products = Product::whereHas('categories', function ($query) use ($category) {
            $query->where('id', '=', $category->id);
        })->when(count($brand_list) > 0, function ($query) use ($brand_list) {
            $query->whereHas('brand', function ($query) use ($brand_list) {
                $query->whereIn('slug', $brand_list);
            });
        })->when($minPrice, function ($query) use ($data) {
            $query->where([
                    ['has_variants', '=', 0],
                    ['price', '>=', $data['active_filters']['min_price']],
                ])
                ->orWhere([
                    ['has_variants', '=', 1],
                    ['min_price', '!=', 0],
                    ['min_price', '>=', $data['active_filters']['min_price']],
                ]);
        })->when($maxPrice, function ($query) use ($data) {
            $query->where([
                ['has_variants', '=', 0],
                ['price', '<=', $data['active_filters']['max_price']],
            ])
                ->orWhere([
                    ['has_variants', '=', 1],
                    ['max_price', '!=', 0],
                    ['max_price', '<=', $data['active_filters']['max_price']],
                ]);
        })->when($orderPrice, function ($query) use ($orderPrice) {
            $query->orderBy('price', $orderPrice);
        })->when(!$orderPrice, function ($query) {
            $query->orderBy('id', 'desc');
        })->when($stockOrder, function ($query) use ($stockOrder) {
            if($stockOrder == 'in_stock') {
                $query->where('quantity', '>', 0);
            } else if($stockOrder == 'no_stock') {
                $query->where('quantity', '=', 0);
            }
        })->when($saleOrder, function ($query) use ($saleOrder) {
            if($saleOrder == 'on_sale') {
                $now = time();
                $query->where([
                    ['sale_price', '>', 0],
                    ['sale_start', '<', $now],
                    ['sale_end', '>', $now],
                ])->orWhere([
                    ['sale_price', '>', 0],
                    ['sale_start', '=', 0],
                    ['sale_end', '=', 0],
                ]);
            } else if($saleOrder == 'no_sale') {
                $query->where('sale_price', '=', 0);
            }
        })->when($featuredOrder, function ($query) use ($featuredOrder) {
            if($featuredOrder == 'featured') {
                $query->where('featured', '=', 1);
            } else if($featuredOrder == 'not_featured') {
                $query->where('featured', '=', 0);
            }
        })->when(count($activeColors) > 0, function ($query) use ($activeColors) {
            $query->whereHas('colors', function ($query) use ($activeColors) {
                $query->whereIn('value', $activeColors);
            });
        })->when(count($activeSizes) > 0, function ($query) use ($activeSizes) {
            $query->whereHas('sizes', function ($query) use ($activeSizes) {
                $query->whereIn('value', $activeSizes);
            });
        })->with(['colors', 'sizes', 'reviewsCount'])->get();

1 个答案:

答案 0 :(得分:2)

我认为这里的问题是orWhere。当你使用orWhere时,你应该总是用额外的封闭包裹结构。我们来看看:

->when($minPrice, function ($query) use ($data) {
            $query->where([
                    ['has_variants', '=', 0],
                    ['price', '>=', $data['active_filters']['min_price']],
                ])
                ->orWhere([
                    ['has_variants', '=', 1],
                    ['min_price', '!=', 0],
                    ['min_price', '>=', $data['active_filters']['min_price']],
                ]);
        })

这部分可能看起来像这样:

->when($minPrice, function ($query) use ($data) {
      $query->where(function($query) use ($data) {
            $query->where([
                    ['has_variants', '=', 0],
                    ['price', '>=', $data['active_filters']['min_price']],
                ])
                ->orWhere([
                    ['has_variants', '=', 1],
                    ['min_price', '!=', 0],
                    ['min_price', '>=', $data['active_filters']['min_price']],
                ]);
          });
        })

如上所述,整个where .. orWhere被包裹在另外的封闭中。

原因很明显。如果你没有额外的闭包,你可以产生这样的查询

WHERE A and B or C and D or E

通常它应该是这样的:

WHERE A and (B or C) and (D or E)

所以当你使用闭包时,他们会在查询中添加额外的括号以使其有效。

同样地,您应该显然包装所有其他where ... orWhere构造以使其按预期工作