In my Model Products have
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');
}
In my Model Attributes have
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');
}
In my Model AttributesValues
public function productAttribute()
{
return $this->belongsTo(Attributes::class, 'attribute_id');
}
In my Model TypeAttribute
public function attributes()
{
return $this->hasMany(Attributes::class, 'type_atributte_id');
}
My controller Product(show)
$product = Products::findOrFail($id);
return view('public.product', compact('product'));
In my product.blade have
@if ($product->attributes->isNotEmpty())
<h2>Atributos:</h2>
<ul>
@foreach ($product->attributes as $attribute)
{{ $attribute->typeAttribute->id }}
<li>{{ $attribute->titulo }}:
@foreach ($attribute->attributeValues as $value)
{{ $value->valor }}
@if (!$loop->last), @endif
@endforeach
</li>
@endforeach
</ul>
@endif
But it is printing duplicate attribute title
In a one product have Color: Rojo, Verde but printing all values color and duplicated
Atributos:
3
Color: Rojo , Verde
3
Color: Rojo , Verde
In other product only have talla-Large and color-Rojo but printing all values color, talla and duplicated
Atributos:
3
Color: Rojo , Verde
2
Talla: Small , Large
Necesito ayuda porfavor