Pivot table with 3 models in laravel


I have 3 tables: Draw, player and round, and in turn a table in common for the three having as attributes: id, id_draw, player_id, id_ronda.

What I do not know, is how to put together this type of relationship and how to insert data. Since in a many-to-many relationship with two tables it would be: draw-> player () -> atach (...);

With this type of relationship of 3 tables, I do not know how to insert data and assemble the belongsToMany relation.


The ternary relationship is given by draw - PlayerATP - Round being the resulting table Matches

asked by Juan Pablo B 04.11.2016 в 03:42

1 answer


Bearing in mind that Eloquent does not have support for composite primary keys , I recommend that you raise the structure of your database in a different way to save yourself headaches.

Assuming (I do not know if it will be like that, but it can be adapted) that a round has several rolls (draws) and a roll belongs to a player, you can build some relationships:

player 1 - N draw (player hasMany draws, draw belongsTo player)

round 1 - N draw (round hasMany draws, draw belongsTo round)

Provide more information about the relationships you want to make so that I can show you a more complex example.



It's tennis ... - a Draw has many players and many rounds. - A player will be in many draws and many rounds. - A round will be in many draws and will have many players.

draw M - N player (player belongsToMany draw, draw belongsToMany player)

draw M - N round (draw belongsToMany round, round belongsToMany draw)

player M - N round (player belongsToMany round, round belongsToMany player)

There are three relationships with the pivot table between them, but more information is needed, since you are using a ternary but you have not provided enough details to know what you are trying to do.

answered by 04.11.2016 / 13:56