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

mstnorris's avatar

Games and Players (Users) for Table Tennis scores app (simple)

I have two models, Game, and User (a player).

In my games table, I store the ID of player_one_id, player_two_id, along with their scores, and the date that they played.

Firstly, I would like to know how to set up the relationships as a player (User) can either be player one or player two so it isn't a straight forward hasMany relationship.

Secondly, I would also like to know how to group by the day that games were played on when displaying all games. So for example:

Tue 29 Dec 2015

Mike 18 - 21 John

John 19 - 21 Ben

Mon 28 Dec 2015

Mike 21 - 19 John

0 likes
8 replies
Pendo's avatar

I'm not sure, but I think you have to set two hasMany relationships in the User model and naming the foreign keys explicitly. Like so:

function playerone() {
    return $this->hasMany('App\Games', 'foreign_key', 'local_key');
}


function playertwo() {
    return $this->hasMany('App\Games', 'foreign_key', 'local_key');
}

However, this won't group the results, but since they come from both datasets they should be easy to merge together (https://laravel.com/docs/master/collections#method-merge).

GroupBy is a method you can use on an Eloquent collection if I recall correctly (https://laravel.com/docs/5.1/collections#method-groupby)

toniperic's avatar

@mstnorris considered using a pivot table? That way you can avoid the "player_one_id" and "player_one_score", and the player_two variations.

Example:

  • games table has only id column for this purpose, and the timestamps
  • users table has id and name columns
  • game_user table has game_id, user_id and score columns.

This way, if you and I played a table tennis game, it would be stored in the games table with id of 1. In the users table there would be two entries - mstnorris and toniperic, with id's 1 and 2 respectively. In the game_user table there would be two entries - first entry would be your score for that game, that is:

  • game_id = 1
  • user_id = 1 (your id)
  • score = 1

the second entry in the game_user table would be my score for the game, that is:

  • game_id = 1
  • user_id = 2 (mine id)
  • score = 3

So, essentially, I beat you 3 to 1 in a table tennis game. :)

This should scale well and give you more flexibility, especially since you could fetch all the games that have been played each day, and then eager load the necessary details (such as players who have participated, scores etc).

3 likes
wing5wong's avatar

This is the approach i took with a scoring app for basketball games. It works out well, and you can group the games by the day the are played on easily.

I created my own model for the game_player though rather than using a default pivot.

i would post my models but am away on vacation sorry!

toniperic's avatar

@mstnorris for people coming from Google and other sources, would be great if you actually posted your solution as well so we have it all in one place.

mstnorris's avatar

Thanks everyone, I'm still working on it but will return here once I'm done :)

@toniperic how do I group by the date (day) the games were played?

Please or to participate in this conversation.