Answer the question
In order to leave comments, you need to log in
How to organize work with related models in Eloquent when creating a REST api?
Please tell me how to organize the work?
If I understood correctly from the sources and documentation - we have several types of relationships
BelongsTo - The service is in the category
HasOne / HasMany - The category contains one / several services
BelongsToMany - The user is "in" several roles, the relationship is through the 3rd table
HasOneThrough/HasManyThrough - The role contains one / several users, the connection through the 3rd table
MorphOne / MorphMany - the same as HasOneThrough / HasManyThrough, only with a condition, that is, it will work only for a certain value in the column
MorphToMany - interaction of four tables. If MorphMany meant multiple one-to-many in one table, then it is multiple MorphMany with a list in the fourth table. Fierce game
Pivot - Systemic. When we go over the list of users obtained from the role through the relationship, the Pivot is the
MorphTo - System link table. When working with Morph, it links the table so that MorphOne/MorphMany work.
In addition to the fact that there is a fierce cloud of these links, as it turned out in the code, they still don’t know how to save everything, as if they were made only for reading and filtering.
For example
, belongsTo() - can associate() / dissociate() - essentially put the ID in its place, then you need to call save(). This seems to be correct.
hasOne/hasMany()- only saveOne()/saveMany() can do it - that is, it does not establish a connection, but immediately writes to the database, putting down the IDs for the campaign, but the trick is that if there is no parent model, then the IDs will not be put down, they arrived , that is, she does not change the model, it immediately writes, then issues with transactions, when you want to do a recursive save
belongsToMany() - can sync() and attach(), attach - add an entry in the linked table even if there is one, detach() - remove entries from the list , sync() - remove unnecessary and add which was not. And it does it immediately in the database. This means that it also needs to be called in the service, there is no such concept as "build a model with links according to the ATT".
the rest of the connections do not know how, it seems to me, to save, but only to read - there are some sketches on saving, but they do not put down foreign keys, but stupidly work as if I called save () on the model itself.
I originally wanted to rewrite the push() method that saves the model and its links recursively, but since the links work like hell, it almost doesn't make sense.
===
I'm asking why... If I'm trying to make a RestController that doesn't give any model - it can CRUD + a little more, then I need to form the model with which I work, and then save it in a transaction. Immediately it turns out that everything is sharpened for the fact that each model needs to write 5 controller actions and in each action call its own save logic, which is written almost in `for`s.
Am I understanding correctly, or is there an elegant way to take off with this?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question