2 years ago

Pivot Tables and eloquent.

Posted 2 years ago by danielesposito

Hello to everyone, this is maybe the hardest problem I'm facing since I use laravel and I hope some of you might help me to solve it.

So, I have the following schema in my database:


  1. id_game


  1. id_player


  1. id_role
  2. description


  1. role_id
  2. game_id
  3. player_id

Now, I have a many to many relationships with games and players and the following model

class Games {

public function players {

    return $this->belongsToMany(Players::class, Games_players,  id_game, id_player)     



This will return all the players associated with games, but how can I return the role of the players in this game? I know that I have an object $games->players->pivot where I can add the column role_id, but how can I get for example the description using eloquent? I'm struggling to get this work.

I will appreciate any help.



Please sign in or create an account to participate in this conversation.