8

Sorry about the non-explanatory title but I could not come up with a descriptive one.

I've got the following 3 tables: - games - platforms - games_platforms

And I've got 2 Models in Laravel for both Platform and Game.

public function games() 
{
    return $this->belongsToMany('App\Game', 'games_platforms')->withPivot('release_date');
}

public function platforms() 
{
    return $this->belongsToMany('App\Platform', 'games_platforms')->withPivot('release_date');
}

Now this works like a charm, I get a JSON string with all the information in the 3 tables, like this.

[{
    "id": 1,
    "name": "Borderlands",
    "short_description": "",
    "created_at": null,
    "updated_at": null,
    "platforms": [{
        "id": 4,
        "name": "PC",
        "pivot": {
            "game_id": 1,
            "platform_id": 4,
            "release_date": "2016-03-03"
        }
    }]
}]

Now my problem is as follows. I don't want to show the whole 'pivot' information, just the 'release_date', like this:

"platforms": [{
        "id": 4,
        "name": "PC",
        "release_date": "2016-03-03"

Is there an easy way in Laravel to do such a thing? As far as I can see right now, looking at other posts, is to either write a function that turns the json into an array and I can arrange it then. Or I can write my own query instead of letting Laravel do all that.

Hope you guys can help me with this issue. Thanks!

jszobody
  • 28,495
  • 6
  • 61
  • 72
Serellyn
  • 405
  • 1
  • 9
  • 26
  • I know Laravel supports eager loading out of the box with very comprehensable syntax. I've never used it on pivot tables, but maybe give that a try? Someone did it here and the answer seemed to help: https://laracasts.com/discuss/channels/eloquent/eager-loading-pivot-tables Also this previous question seems to have a very decent answer: http://stackoverflow.com/questions/21645257/generating-clean-formatted-json-with-laravel-pivot-tables?rq=1 – Loek Jun 16 '16 at 14:17

3 Answers3

6

I would modify the data returned from the query via methods on the collection class:

//replace Game::all() with your actual query
return Game::all()->each(function($game){
    $game->platforms->map(function($platform){
        $platform->release_date = $platform->pivot->release_date;
        unset($platform->pivot);
        return $platform;
    });
});
Steve
  • 20,703
  • 5
  • 41
  • 67
1

I know this is already answered but I believe the proper answer would be to add whatever you want to hide to your hidden attribute on the model.

<?php
class Games extends Eloquent
{
    protected $hidden = ['pivot.game_id', 'pivot.platform_id'];
}

I am not sure what your keys are becuase they are different in your two examples. Please see: https://github.com/laravel/framework/issues/745

Bill Garrison
  • 2,226
  • 3
  • 34
  • 75
0

A better way is to use Laravel resources,

First create Resource (php artisan make:resource)

Rresource GameResource extends Resource
{
 public function toArray($request)
 {
  return [
    'release_date' => $this->pivot->release_date,
    'name' => $this->name,
     /// All other fields or you can merge both here 
   ];
 }
}

Now use this resource;

$data = GameResource::collection(Game::all());
Shahid Karimi
  • 4,096
  • 17
  • 62
  • 104