Answer the question
In order to leave comments, you need to log in
How to link three tables?
Goodnight. Such situation. There are tables: accessories (products), spare parts (details), orders. So, in orders there is a product_id field, where the id of the ordered product is stored. So. What is trouble. With belongsToMany it works for products, but not for spare parts, because no detail_id field. I don't know. I only have one option in mind right now. Make a table of orders additionally for spare parts and output. Or is it possible to somehow do this universally in one? Who has any ideas?
Answer the question
In order to leave comments, you need to log in
n In addition to customizing the name of the joining table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to:
return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');
Isn't it?
Hey! Please tell me how you build logic? One accessory can be in several orders, and also one order can contain several accessories. I understand correctly? For such a many-to-many relationship, you need to create a new table products_orders, where id, product_id, order_id will be stored. And for such a table in Laravel, make belongsToMany relationships. If the logic is different, describe it :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question