There is such a structure: Attributes_group -> has many -> attributes -> has many -> attributes_value. When adding a product, I display all these tables, and mark the necessary attributes_value. They are sent to the server array. It looks like this: Screen with characteristics

How can they be combined with the product, so that later in the template it would be possible to output them like this:

foreach($product->attributes_group as $group){ $group->name foreach($group->attributes as $attribute){ $attribute->name foreach($attribute->value as $value){ $value->val } } } 

upd: added the 'product_attribute' table. In this table, the product ID, and the attribute value ID (which is sent to the server in an array). But only the value is displayed in the product template. But it is understandable why. But I don’t know how to combine products with characteristics that would turn out like this - Product-> Attribute_group-> attribute-> attribute_value.

  • Have you ever tried to do any join? - Connor Holt
  • @ConnorHolt tried it. Now did so (but this is not at all correct): added the 'product_attribute' table. In this table, the product ID, and the attribute value ID (which is sent to the server in an array). But only the value is displayed in the product template. But it is understandable why. But I don’t know how to combine products with characteristics that would turn out like this - Product-> Attribute_group-> attribute-> attribute_value - Alex_01

1 answer 1

 Product::with('attributes_group', 'attributes_group.attributes', 'attributes_group.attributes.attributes_value')->get(); 

https://laravel.com/docs/5.3/eloquent-relationships#eager-loading

PS Remove the underscore in the model / table name and change it to AttributesGroup

  • Thank you for the answer. But such a request does not come out, because in the product_attributes pivot table, only product_id and attribute_value_id. It turns out the opposite. Ie, first all attribute values ​​are displayed, then the attributes themselves, and then attribute groups. And the opposite is necessary) - Alex_01
  • You can make a select and then regroup into the desired array as an option - apelsinka223
  • can i see an example implementation? I just do not quite understand how to do it. - Alex_01