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

SUPAD's avatar

Relationship many to many to many (a 3 way, but not the one you wish for ;))

Hello everyone,

This is my first question so i'm sorry if don't respect any formating rules or description style. :)

I have a "clean code" issue : i found a way to do what i want but i'm not sure that it's the correct Laravel's way.

I'm working on a quiz game, so i have players who play games, wich are made of several questions, each one having multiple possible answers :

PLAYERS
id
pseudo

GAMES
id

QUESTIONS
id
question_text

ANSWERS
id
question_id
answer_text

Having the relationship that links a player to a game and the relationship that links questions to a game was easy : both are belongsToMany/belongsToMany.

My issue is with player's answers : when a player choose an answer, i want to save it. But since a player can give the same answer into several different games, a "player's answer" is defined by :

  • The answer id
  • The player id
  • The game id

How to do this ?

So far, i'm planning to create a model PlayerAnswers and creating "manually" the method in Game, Player and Question according to my application needs.

What would you do, experienced people of Laracast, if you were in my shoes ?

0 likes
1 reply
pmall's avatar
pmall
Best Answer
Level 56

You have to introduce a new model representing a player/game couple :

  • Player hasMany PlayerGame / PlayerGame belongsTo Player
  • Game hasMany PlayerGame / PlayerGame belongsTo Game
  • Answer belongsTo PlayerGame / PlayerGame hasMany Answer

Please or to participate in this conversation.