我正在研究一些产品的过滤器。我有大部分工作,但我遇到了一个不可能的where
条款的错误。Laravel不可能在哪里查询
该表包含单个产品的多行,我试图匹配每个产品的多个标准,这导致它失败。
如果您对此有任何意见或者解决方法,我将不胜感激。
数据库表如下所示:
-------------------------------------------- |id | FilterKey | filterValue | product_id | -------------------------------------------- |1 | Colour | Gunmetal | 1 | |2 | Colour | Silver | 1 | |3 | Size | 750cc | 1 | |4 | Size | 1000cc | 1 | |5 | Colour | Red | 2 | |6 | Colour | Blue | 2 | |7 | Size | 750cc | 2 | |8 | Size | 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
如果选择了屏幕截图,那么只有'产品一'应该出现在页面上。
这就是所谓的一组division.The逻辑遵循的是“得到的产物为其中不存在选择的过滤器,它没有“ – apokryfos