Pivot Tables and eloquent.

Posted 1 year 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:

Games

  1. id_game

Players

  1. id_player

Role_Players

  1. id_role
  2. description

Games_players

  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.

Thanks.

D

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

Reply to

Use Markdown with GitHub-flavored code blocks.