I
I
Ilya2018-06-02 00:34:29
Laravel
Ilya, 2018-06-02 00:34:29

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

2 answer(s)
Y
Yan-s, 2018-06-02
@Yan-s

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

That the problem is not caught. Try to add a more consistent example with scheduled actions and the moment when you have a problem.
I'll try to sort it out. We have goods, their attributes and groups of attributes.
So we should have
- a form for creating attribute groups (a named group is simply created in which you can place an attribute,
- a form for creating attributes (where we can add an attribute and select a group for it from existing ones)
- and a form for adding products (where we we can choose attributes from existing ones)
Seems like no problem. If you want to be able to create a new attribute from the add product form, then this is done with AJAX. There is a button "create a new attribute" which, for example, opens the attribute creation form in the modal.
Accordingly, the product creation controller is only responsible for creating the product, the attributes must already be all created.
PS
By the way, if you saw something like this in some CSM, it will be useful to figure out how it was decided there.

P
pLavrenov, 2018-06-04
@pLavrenov

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 question

Ask a Question

731 491 924 answers to any question