I have a model Productas well as a model Attribute. The relationship is between Productand Attributemany for many. In my model, ProductI am trying to create a dynamic accessory. I am familiar with Laravel accessories and the mutator function described here here . The problem I am facing is that I do not want to create an accessory every time I create a product attribute.
For example, a product may have a color attribute that can be configured as follows:
public function getColorAttribute($value)
{
foreach ($this->productAttributes as $attribute) {
if ($attribute->code === 'color') {
return $attribute->pivot->value;
}
}
return null;
}
After that, the color of the product can be obtained in this way $product->color. If I need to add an attribute sizeto the product, I will need to set up another accessor in the model Product, so I can access it as follows: $product->size.
"" ?
Laravel ?