public function attributeValues()
{
return $this->hasMany(AttributesValues::class, 'product_id');
}
public function attributes()
{
return $this->belongsToMany(Attributes::class, 'attribute_product_values', 'product_id', 'attribute_id')
->withPivot('attribute_value_id');
}
В атрибутах моей модели есть
public function attributeValues()
{
return $this->hasMany(AttributesValues::class, 'attribute_id');
}
public function typeAttribute()
{
return $this->belongsTo(TypeAttribute::class, 'type_atributte_id');
}
public function products()
{
return $this->belongsToMany(Products::class, 'attribute_product_values', 'attribute_id', 'product_id')
->withPivot('attribute_value_id');
}
В моих атрибутах и значениях модели
public function productAttribute()
{
return $this->belongsTo(Attributes::class, 'attribute_id');
}
В атрибуте TypeAttribute моей модели
public function attributes()
{
return $this->hasMany(Attributes::class, 'type_atributte_id');
}
Мой контроллер (показать)$product = Products::findOrFail($id);
return view('public.product', compact('product'));
В моем продукте.blade есть
@if ($product->attributes->isNotEmpty())
Atributos:
- @foreach ($product->attributes as $attribute)
{{ $attribute->typeAttribute->id }} - {{ $attribute->titulo }}:
@foreach ($attribute->attributeValues as $value)
{{ $value->valor }}
@if (!$loop->last), @endif
@endforeach
@endforeach
Но заголовок атрибута печатается дубликат.
В одном продукте есть Цвет: Rojo, Verde, но все значения печатаются цветом и дублируются:
п>
Atributos:
3
Color: Rojo , Verde
3
Color: Rojo , Verde
В другом продукте есть только Talla-Large и Color-Rojo, но печатаются все значения Color, Talla и дублируются:
Atributos:
3
Color: Rojo , Verde
2
Talla: Small , Large
Подробнее здесь: https://stackoverflow.com/questions/784 ... uct-detail