Answer the question
In order to leave comments, you need to log in
Laravel Eloquent how to properly organize the form for adding a product with many attributes and options?
Greetings to all
Google, I have been googling for many hours along with YouTube visual parsing. I can not find information on how to organize a rather complex form for adding and editing the "product" entity, which has many different relations. Everywhere there are some simple examples with an admin panel a la Voyager and a form for one table into which all the product attributes are crammed along with images. 1 time saw with options (just one more plate with the fixed attributes in the form of columns of the table color, price etc.)
There was a stupor. I have very little experience, but I try to do it as correctly as possible, even if slowly, and use Laravel features, and not $_POST server variables instead of passing Request. =) to make it clear what I mean.
There is a standard EAV database model (some columns are removed for a pleasant perception):
Product (id, title, group_id, is_published[1,0], timestamps)
Attribute (id, type[select,checkbox,text], name)
product_attributes (id, product_id, attribute_id, value) //
pivot_table the same as the type of goods in many CMS or just groups of characteristics associated with the product) One attribute can be in many groups
Groups (id, name) // id = 1; name = rings
group_attributes (attribute_id, group_id)
I will not describe the options here, there are 2 more tables with one pivot
In general, the idea is nothing superfluous, but I can’t imagine how to add it all at the same time by clicking on the save button. After all, there are many dependencies, for example, we don’t know what attributes the product will have without selecting a pack of attributes or linking a group to the product, in another way. Let groups be added in advance. It is not advisable to change the group in the product, because then you need to overwrite all the characteristics, the group will change and the values of the departed characteristics will remain (with update) when adding, in principle, do not care about it. And a lot of such nuances. I will describe them all, but in theory there should be some kind of control center for such a form, or everything should be shove into the controller method
There are Attribute, Product, Group models. I don't know how to do it right. Maybe someone has an example of such forms. And I still don’t know whether it’s worth sending the entire form to one controller route, or maybe it’s necessary to somehow redistribute tasks (atomicity and all that). There are links for everything. Maybe some interface should be written for this.
Answer the question
In order to leave comments, you need to log in
but I can’t imagine how to add all this at the same time by clicking on the save button. After all, there are many dependencies, for example, we don’t know what attributes the product will have without selecting a pack of attributes or linking a group to a product, otherwise
The Laravel documentation has a "Relationships" section
https://laravel.ru/docs/v5/eloquent-relationships
It clearly describes how to create relationships and how to add records through them.
In this case:
example #1 is many to many (ManyToMany).
example #2 is one to many (OneToMany)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question