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

Luu*_*gen 6 php database laravel eloquent

我正在为我的laravel应用程序构建一个分层导航模块 - 就像在Magento或WooCommerce中完成的那样.这是一个想法:产品可以分配单个或多个属性,然后用户应该能够使用这些属性过滤产品.像属性"材料"一样,可以为产品分配一个或多个值,如,木材塑料.我的问题是我无法弄清楚如何正确地做到这一点.

我的数据模型是这样的:

         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    
Run Code Online (Sandbox Code Playgroud)

我在产品产品选项模型之间设置了多对多关系(因此是数据透视表).

在我的视图文件中,将显示一个表单,该表单循环遍历所有不同的属性,然后遍历其选项,并为每个选项创建一个复选框,其值为id.理想情况下,这些值在一个数组中组合在一起,并命名如下: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..
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

--------编辑/补充资料--------

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

$products = $products->whereHas('attributeOptions', function($query)
{
    $query->where(function($query)
    {
        $query->where('value', 'iron');
    });
});
Run Code Online (Sandbox Code Playgroud)

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

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

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

产品attribute_options之间的关系是:

public function attributeOptions() {

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

}
Run Code Online (Sandbox Code Playgroud)

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

在此先感谢,我很乐意向您学习!

luk*_*ter 2

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

如何在SQL中实现过滤系统?

将 MySQL EAV 结果检索为关系表的最佳性能是什么


虽然我认为这在性能方面效果不佳(具有许多属性和产品),但这里有一个应该适合您当前设置的解决方案:

将您的复选框更改为:

<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..
Run Code Online (Sandbox Code Playgroud)

这样,同一过滤器的多个值将作为数字数组添加到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();
Run Code Online (Sandbox Code Playgroud)