php – Laravel不可能在哪里查询

php – Laravel不可能在哪里查询,第1张

概述我正在研究某些产品的过滤器.我有大部分工作但是我遇到了一个不可能的where子句的错误. 该表包含单个产品的多个行,我尝试匹配每个产品的多个条件,这会导致它失败. 如果您对此有意见,或者可能是解决此问题的方法,我将非常感激. 数据库表如下所示: -------------------------------------------- |id | FilterKey | filte 我正在研究某些产品的过滤器.我有大部分工作但是我遇到了一个不可能的where子句的错误.

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

如果您对此有意见,或者可能是解决此问题的方法,我将非常感激.

数据库表如下所示:

    --------------------------------------------    |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

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

解决方法 你认为你添加的范围是错误的.在我看来,即使你的数据库结构也不正确.这是我如何构建这个:

过滤表

该模型将包含所有过滤器值.例如,颜色,大小等.以下是过滤表的结构:

-----------------|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          |--------------------------------------------

现在您只需查询过滤器表,然后获取所有过滤器的关联产品.之后,您只需编译一个独特产品列表.

Unqiue产品基于选定的过滤器.

$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
总结

以上是内存溢出为你收集整理的php – Laravel不可能在哪里查询全部内容,希望文章能够帮你解决php – Laravel不可能在哪里查询所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1248846.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存