Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 如何唯一地显示属性类型的属性和值?_Laravel - Fatal编程技术网

Laravel 如何唯一地显示属性类型的属性和值?

Laravel 如何唯一地显示属性类型的属性和值?,laravel,Laravel,所以我有一个属性模型,它与产品模型有多对多的关系。因此,每个产品都可以有许多属性,反之亦然 属性表: 产品表 这只是简单的产品细节、价格等,没有什么特别之处 产品属性表-透视表 在我的控制器中,我获得了所有类似产品的功能。这基本上只是获取数据透视表数据: $features = DB::table('product_attributes') ->distinct('attribute_id') ->whereIn('sku_prod

所以我有一个属性模型,它与产品模型有多对多的关系。因此,每个产品都可以有许多属性,反之亦然

属性表:

产品表 这只是简单的产品细节、价格等,没有什么特别之处

产品属性表-透视表

在我的控制器中,我获得了所有类似产品的功能。这基本上只是获取数据透视表数据:

$features = DB::table('product_attributes')
            ->distinct('attribute_id')
            ->whereIn('sku_product_id', $skuProductIDs)->get();```
在视图中,我有以下代码:

@foreach(\App\Models\Attribute::all() as $attribute)

    <div class="my-3">
        <label class="font-hk text-secondary mb-2">{{ $attribute->name }}</label>
            <div class="my-1">
                @foreach($features as $feature)

                    @if($feature->attribute_id === $attribute->id)
                        <input type="checkbox" class="form-checkbox h-5 w-5 text-gray-600 feature-checkbox" name="feature" data-id="{{ $feature->value }}">
                        <span class="ml-2 text-gray-700">{{ $feature->value }}</span>
                                                
                    @endif
                @endforeach
            </div>
    </div>

@endforeach
@foreach(\App\Models\Attribute::all()作为$Attribute)
{{$attribute->name}
@foreach($features作为$feature)
@如果($feature->attribute\u id===$attribute->id)
{{$feature->value}
@恩迪夫
@endforeach
@endforeach
问题是im没有收到错误,但是如果不同的产品具有相同的属性,例如,两部手机可能具有颜色
白色
,则该值在过滤器中出现两次,如下所示:


我如何调整它,使重复的值只出现一次?

首先,您需要创建一个所有属性的列表,然后再简单地应用

collect( $attributes )->unique()
此集合将提供唯一的属性列表,您可以根据自己的逻辑添加一个计数,例如xyz(3)等