lundi 28 mai 2018

Get collection with own data and secified vars from sub collection in eloquent

First of all, sorry when the question description does not describe my question very well but i have no idea how to form my question better in one line. Suggestions are welcome..

Background info

  • Laravel 5.6.x
  • I defined my objects as eloquent models
  • Using migrations to create the database

Background info about the application

I have an application where a Game object has a one-to-many relation with Player objects. The Game object contains all game details, the Player object contains all details of an individual player.

Players gets invited for a game and can accept of deny this. This info is saved in the player themselves as the player is unique per game.

Now i want to create an overview of all games. One of the things in the overview are the amount of accepted players, the invited players and their id's.

To get all games, i use $games = Game::all(); what gives me an array with all games (without players). I was able to add the players to the array by doing:

foreach ($game as $singleGame){
   $singleGame->players
}

After that my $games array contains all games with a subcollection per game with its players. This is almost what i need, but the subcollection contains to many details about the player, e.g. the hand cards the player has.

The question Is there a simple way to just add the invite_status and player_id value of each player to the game its sub-collection instead of all the players vars?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2IT8XGA
via IFTTT

Aucun commentaire:

Enregistrer un commentaire