简体   繁体   English

卡在laravel查询中

[英]Stuck with laravel query

I'm trying to write a filter system using laravel, here is the situation: 我正在尝试使用laravel编写过滤器系统,情况如下:

Here is my $inputs array. 这是我的$inputs数组。 Colors are checkboxes, and material is a dropdown : 颜色是复选框,材料是下拉列表:

$inputs = [
    'colors' => ['green' => 'green', 'red' => 'red'],
    'material' => 'wood',
]

Here is the table ( variants_values ) I'm looking at. 这是我正在查看的表( variants_values )。 This is where I stock all combination of properties for my variants : 在这里,我可以存储所有变体的所有属性组合:

id  | product_id | variant_id | property_id |   label   | value
550           91          213             1    color      green
551           91          213             2    material   wood
552           91          214             1    color      red
552           91          214             2    material   plastic

Here is my request. 这是我的要求。 The goal is to return corresponding products, so I use a whereHas() method to make a subquery over my variants_values : 目标是返回相应的产品,因此我使用whereHas()方法对我的variants_values进行子查询:

    foreach($inputs as $key => $input) {
        $products->whereHas('variants_values', function($query) use($key, $input) {
            if(is_array($input)) {
                $query->where('label', $key)->whereIn('value', $input);
            } elseif(!empty($input)) {
                $query->where('label', $key)->where('value', $input);
            }

        });
    }

This code works quite well except one situation: 该代码在一种情况下工作得很好:

  • If I check green color and wood material, I want that my product 91 is returned, which is the case. 如果我检查greenwood材料,那么我希望退回我的产品91

BUT

  • If I check red color and wood material, I want no results because the red variant is made of plastic , not wood . 如果我检查redwood材料,我不会有任何结果,因为red变体是plastic而不是wood

Actually, the product 91 is still returned because actually, my code don't check if it's the same variant that correspond to the $inputs ; 实际上,产品91仍返回,因为实际上,我的代码不检查它是否与$inputs对应的变体相同;

So I need something in my code that would say : 所以我需要在代码中说些什么:

foreach($inputs as $key => $input) {
    $products->whereHas('variants_values', function($query) use($key, $input) {
        if(is_array($input)) {
            $query->where('label', $key)->whereIn('value', $input);
        } elseif(!empty($input)) {
            $query->where('label', $key)->where('value', $input);
        }
        ##
        $query->where('Verify that the variant ID is the same or something like that');
        ##
    });
}

I know I'm not far from the solution but I'm stuck here actually. 我知道我离解决方案不远,但实际上我被困在这里。

Thank you for your help 谢谢您的帮助

It looks like this requires a more complex query. 看来这需要更复杂的查询。

Put this in your Product model: 将其放入您的Product模型中:

public function variants() {
    return $this->belongsToMany(Variant::class, 'variants_values');
}

Copy the variants_values relationship to your Variant model. variants_values关系复制到您的Variant模型。

Then try this: 然后试试这个:

$products->whereHas('variants', function($query) use($inputs) {
    foreach($inputs as $key => $input) {
        $query->whereHas('variants_values', function($query) use($key, $input) {
            $query->whereColumn('product_id', 'products.id');
            if(is_array($input)) {
                $query->where('label', $key)->whereIn('value', $input);
            } elseif(!empty($input)) {
                $query->where('label', $key)->where('value', $input);
            }
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM