私はいくつかの製品のフィルタに取り組んでいます。私はそれの大部分は動作していますが、不可能なwhere
句でエラーに遭遇しています。Laravel impossible query
表には1つの製品の複数の行が含まれていますが、製品ごとに複数の基準を一致させようとしていますが、これは失敗につながります。
これについて意見がある場合、またはこれを修正する方法があれば、大変感謝します。
データベーステーブルは次のようになります。
-------------------------------------------- |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
このオプションを選択すると、スクリーンショットのように、「product one」のみがページに表示されます。
これは、「製品を取得されて従って、セットdivision.Theロジックと呼ばれていることを" – apokryfos