【问题标题】:Laravel impossible where queryLaravel 不可能在哪里查询
【发布时间】:2018-03-31 14:11:47
【问题描述】:

我正在为某些产品开发过滤器。我的大部分工作都在工作,但是我遇到了一个不可能的错误where 子句。

该表包含单个产品的多行,我试图匹配每个产品的多个条件,这导致它失败。

如果您对此有意见,或者可能有解决此问题的方法,我将不胜感激。

数据库表如下所示:

------------------------------------------ |标识 |过滤键 |过滤器值 |产品编号 | ------------------------------------------ |1 |颜色 |枪色 | 1 | |2 |颜色 |银 | 1 | |3 |尺寸 | 750cc | 1 | |4 |尺寸 | 1000cc | 1 | |5 |颜色 |红色 | 2 | |6 |颜色 |蓝色 | 2 | |7 |尺寸 | 750cc | 2 | |8 |尺寸 | 1000cc | 2 | ------------------------------------------

过滤器看起来像这样:

public function scopeFilterProduct($query, $filters)
{
    $this->filters = $filters;
    if (count ($this->filters) === 1 && isset($this->filters[0]))
    {
        return $query;
    }
    $query->join('product_filters', 'products.id', '=', 'product_filters.product_id')->Where(function($query){
        foreach ($this->filters as $filter => $vals)
        {
            $this->filter = $filter;
            $this->vals = $vals;
            $query->Where(function ($query){

                $query->Where('filterKey', $this->filter); 

                $query->Where(function($query){
                    foreach ($this->vals as $val){

                        $query->orWhere('filterValue', $val); 
                    }
                    $this->vals = null;
                });

            });
            $this->filter = null;
        };
    }); 
    return $query;
}

然后输出以下 SQL 语句:

select
  distinct
    `products`.`id`
  , `product_id`
from
 `products`
inner join
 `product_filters`
on
 `products`.`id` = `product_filters`.`product_id`
where 
     (
         (`filterKey` = 'Colour' and (`filterValue` = 'gunmetal'))
       and
         (`filterKey` = 'Size' and (`filterValue` = '750cc'))
     )
     and
       `products`.`deleted_at` is null

如果选中,如屏幕截图所示,则页面上应仅显示“产品一”。

【问题讨论】:

  • 这称为集合划分。遵循的逻辑是“获取不存在选定过滤器的产品”

标签: php mysql laravel


【解决方案1】:

我认为您添加的范围是错误的。在我看来,即使您的数据库结构也不正确。这是我的结构:

过滤器表

此模型将保存所有过滤器值。例如,ColourSize 等。过滤表的结构如下:

-----------------
|id | name      |
-----------------
|1  | Colour    |
|2  | Size      |
-----------------

所以你的雄辩模型变成了这样:

class Filter extends Model
{
    protected $fillable = ['id', 'name'];

    public function products()
    {
        return $this->belongsToMany(Product::class, 'products_filters');          
    } 
}

产品表

您的产品型号变为:

class Product extends Model
{
    public function filters()
    {
        return $this->belongsToMany(Filter::class, 'products_filters');          
    } 
}

products_filters 表

经过上述更改后,表格的结构如下:

--------------------------------------------
|id | filter_id | filterValue | product_id |
--------------------------------------------
|1  | 1         | Gunmetal    | 1          |
|2  | 1         | Silver      | 1          |
|3  | 2         | 750cc       | 1          |
|4  | 2         | 1000cc      | 1          |
|5  | 1         | Red         | 2          |
|6  | 1         | Blue        | 2          |
|7  | 2         | 750cc       | 2          |
|8  | 2         | 1000cc      | 2          |
--------------------------------------------

现在您可以简单地查询过滤器表,然后获取所有过滤器的关联产品。之后,您只需编制一份独特产品列表。

根据所选过滤器取消产品。

$ids = [];
$products = new \Illuminate\Support\Collection();

foreach($filters as $filter) {
    foreach($filter->products as $product) {
        if(!in_array($product->id, $ids)) {
            $ids[] = $product->id;
            $products->push($product);
        }
    }
}

return view('results', compact('products'));

在你看来,你需要写:

@foreach($products as $product)
// Your product block HTML
@endforeach

【讨论】:

  • 谢谢拉扎,我现在正在考虑实施这个。希望这将解决它。为了提供帮助,您如何考虑将产品拉到恰好匹配 2 个过滤器的位置?
  • 更新了我的答案以描述我将如何根据提供的产品过滤器过滤独特的产品。
猜你喜欢
  • 2015-07-29
  • 2018-09-08
  • 2016-07-30
  • 2019-04-27
  • 2017-04-17
  • 1970-01-01
  • 2021-06-30
  • 2013-08-21
  • 1970-01-01
相关资源
最近更新 更多