首页 文章

Laravel:在多个值上过滤多对多

提问于
浏览
6

我正在为我的laravel应用程序构建一个分层导航模块 - 就像在Magento或WooCommerce中完成的那样 . 这是个主意: Products can be assigned single or multiple attributes and users then should be able to filter the products down by using these attributes . 与属性"material"类似,可以为产品分配一个或多个值,如铁,木材和塑料 . 我的问题是我无法弄清楚如何正确地做到这一点 .

我的数据模型是这样的:

products table:  id | name         | other info...
                example:  42 | Chair        | ...
                example:  14 | Bike         | ...

       attributes table:  id | name         | description
                example:  02 | Material     | the material that the...

attribute_options table:  id | attribute_id | value    
                example:  11 | 02           | Wood    
                example:  12 | 02           | Iron    

            pivot table:  id | product_id   | attribute_option_id    
                example:  12 | 42           | 11    
                example:  12 | 42           | 12    
                example:  12 | 14           | 12

我在 ProductProduct Option 模型之间设置了多对多的关系(因此是数据透视表) .

在我的视图文件中,将显示一个表单,该表单循环遍历所有不同的属性,然后遍历其选项并创建 checkbox for each of the options with a value of their id's . 理想情况下,这些值在一个数组中组合在一起,并按如下命名: filter[attribute_id][attribute_option_id] . 一个例子:

<input type="checkbox" name="filter[02][11]" value="id"> Wood
<input type="checkbox" name="filter[02][12]" value="id"> Iron
<input type="checkbox" name="filter[xx][xx]" value="id"> Etc..

在提交表单时,所有选定的属性选项值都将发送到服务器,以便处理此信息的路径,并且只应返回满足所有不同条件的产品 .

因此,如果过滤[02] [11]和[02] [12]过滤器,则只返回分配了属性选项“Wood”和“Iron”的产品 .

起初,我认为这很简单,但我认为我不像我想的那样熟练......因为这是一个Laravel问题,我喜欢一个雄辩的风格解决方案!

附:如果我弄乱了(思考我的)数据模型,请告诉我!我还在学习很多关于Web开发的东西,也许我的问题有更好/更清晰的解决方案/方法

-------- EDIT / ADDITIONAL INFORMATION --------

使用以下代码,我可以过滤一个属性

$products = $products->whereHas('attributeOptions', function($query)
{
    $query->where(function($query)
    {
        $query->where('value', 'iron');
    });
});

但由于产品可以具有不同类型的多个属性(如颜色和材料或多种材料),我需要能够设置多个这样的:

$products = $products->whereHas('attributeOptions', function($query)
{
    $query->where(function($query)
    {
        $query->where('value', 'iron');
        $query->where('value', 'wood');
        $query->where('value', 'yellow');
    });
});

但是这段代码不起作用,因为它检查具有多个值的一行而不是检查具有相同product_id的多行上的值 .

productsattribute_options 之间的关系是:

public function attributeOptions() {

    return $this->belongsToMany('AttributeOption', 'products_attribute_options', 'product_id', 'attribute_option_id');

}

这在Eloquent / Laravel中是否可行,或者我是否需要不同的解决方案

Thanks in advance, I'd love to learn from you!

2 回答

  • 2

    我完全同意@astro和@Dave的评论 . 您应该重新考虑数据库设计 . 我将在这里重新发布@Dave中的两个链接,以便他们获得更多其他人的可见性:

    How to implement filter system in SQL?

    What is best performance for Retrieving MySQL EAV results as Relational Table


    虽然我不认为这在性能方面表现良好(有许多属性和产品),但这是一个适用于您当前设置的解决方案:

    将您的复选框更改为:

    <input type="checkbox" name="filter[02][]" value="11"> Wood
    <input type="checkbox" name="filter[02][]" value="12"> Iron
    <input type="checkbox" name="filter[xx][]" value="xx"> Etc..
    

    这样,同一过滤器的多个值将作为数字数组添加到 filter[02][]

    $query = Product::query();
    foreach(Input::get('filter') as $attributeId => $optionIds){
        foreach($optionIds as $optionId){
            $query->whereHas('attributeOptions', function($q) use ($attributeId, $optionId){
                $q->where('attributeId', $attributeId)
                  ->where('attribute_option_id', $optionId);
            }
        }
    }
    $products = $query->get();
    
  • 3

    我将通过使用whereHas方法,然后使用whereIn来隔离包含所需属性的记录,然后将其过滤到只有具有正确数量的属性的记录 .

    // Assuming you have the IDs of the attributes, rather than the value
    $desiredAttributes = [11,12];
    $products = $products->whereHas('attributeOptions', function($query)     
    use($desiredAttributes)
    {
        $query->whereIn('attribute_id', $desiredAttributes);
    }, "=", count($desiredAttributes))->get();
    

    所以这将首先得到所有具有属性11或12的记录,然后过滤到只有有计数的记录(attribute_option_id)== 2

相关问题