Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

el3's avatar
Level 1

Which relation to use for 3 tables (belongsToMany)?

I have 3 tables, to connect data correctly I use a table between 2 tables with data.

It looks exactly so:

There are teams and players, I want to get players of a team. Which relation is there possible? I tried with belongsToMany() but I was not able to figure it correctly.

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

Just use 3 models. Team and Player both have Group in a BelongsTo relationship.

Group has a hasMany relationship with Team and Player

$teamMembers = $team->group()->players()->get();

or $team = Team::with(group.players)->find(1); and then players are nested in the collection $team->group->players

Please or to participate in this conversation.