Support the ongoing development of Laravel.io →
Database Eloquent Laravel

Hi, I am trying to create a game with the tables listed below. There is a sample of the migration files here: Laravel.io example migration sample

The pivot table works well for players joining a game, I have seeded it and return data using Vue and each game lists all the players as it should.

The problem is creating a relation between the pivot table game_user (referred to as participant) and turns. Not sure if i'm going about this the right way, but my thinking is a player (game_user) plays many turns, a one-to-many relation.

I don't really want to create another primary key on the pivot table unless I have to, but I can't seem to connect them without errors been thrown.

So this foreign key will not work

$table->foreign(['game_id', 'game_user_id'], 'game_user_turns_foreign')->references(['game_id,user_id'])->on('game_user')->onDelete('cascade');

Laravel thows this error during the artisan migrate command

 php artisan migrate:fresh --seed

 Illuminate\Database\QueryException 

  SQLSTATE[42000]: Syntax error or access violation: 1239 Incorrect foreign key definition for 'game_user_turns_foreign': Key reference and table reference don't match (SQL: alter table `turns` add constraint `game_user_turns_foreign` foreign key (
`game_id`, `game_user_id`) references `game_user` (`game_id,user_id`) on delete cascade)

Can someone help me out ?

Or maybe suggest another logical way to solve the relation.

The important thing to know is a turn must have a unique link to a game and a participant (game_user).

Here are the tables

Tables:

  Users Table                    Games Table   
   (shortened for example)        (shortened for example)

+-------------------+            +-------------------+
| Entity: users     |            | Entity: games     |
+-------------------+            +-------------------+
| id                |            | id                |
| first_name        |            | start_time        |
| last_name         |            | end_time          |
| display_name      |            | max_no_of_players |
| email             |            | director          |
+-------------------+            | result_id         |
                                 +-------------------+
                                 
  Pivot Table referred           Turns Table                
  as participants or players     +-------------------+         
+-------------------+            | Entity: turns     |                               
| Entity: game_user |            +-------------------+                               
+-------------------+            | id                |                               
| user_id           |            | created_at        |                               
| game_id           |            | updated_at        |                               
+-------------------+            | game_id           |                               
                                 | game_user_id      |                               
                                 | merge             |                               
                                 | purchase_array    |                       
                                 | piece_played      |                       
                                 | piece_action      |                       
                                 +-------------------+                       
 
Last updated by @muldev 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.