17

I am using laravel 4.1 to build an api. I have pivot a table which is working fine. But the response comes with pivot attributes which i don't want. as you will see in my example i have to two tables name: trips and users. I don't want to see pivot table attributes in my response. Here is the example:

[
    {
        "id": 140,
        "name_first": "hasan",
        "name_last": "hasibul",
        "profile_image": "/assets/images/default-profile-img.png",
        "created_at": "2013-09-18 08:19:50",
        "last_login": "2013-12-26 11:28:44",
        "status": "active",
        "last_update": "2013-10-15 13:40:47",
        "google_refresh_token": null,
        "is_admin": 1,
        "updated_at": null,
        "pivot": {
            "trip_id": 200,
            "user_id": 140
        }
    }

This is my User Model:

public function trips(){
        return $this->belongsToMany('Trip');
    }

This is my trip model:

public function users(){
        return $this->belongsToMany('User');
    }

This is my controller:

public function index($tripId)
    {
        $userCollection = Trip::find($tripId)->users;
        return $userCollection;
    }

This is my route:

//get all the users belongs to the trip
Route::get('trips/{tripId}/users', array(
    'as' => 'trips/users/index',
    'uses' => 'TripUserController@index'
));

is there any way i can remove pivot attributes using laravel or i have to use php ?

hasib32
  • 835
  • 1
  • 13
  • 31

4 Answers4

34

Use the $hidden property of the model, you can add attributes or relations to it and the pivot is basicly acts as a relation.

class Foo extends Eloquent
{
    protected $hidden = array('pivot');

    public function bars()
    {
        return $this->belongsToMany('Bar');
    }
}
TLGreg
  • 8,321
  • 3
  • 23
  • 12
  • 4
    @TLGreg How do I hide only specific fields of pivotal? Say, I have `a_id`, `b_id` and `dummy` fields in my pivotal and I'd like to retrieve only `dummy` out of the three. Is there any way for that? – Vishal Sharma Nov 29 '15 at 15:33
  • @VishalSharma do you know how to your question?? – francisco Jan 13 '22 at 20:11
1

If you want to remove just any one column from the response, then you can try something like this:

In you Model:

public function toArray()
{
    $attributes = $this->attributesToArray();
    $attributes = array_merge($attributes, $this->relationsToArray());
    unset($attributes['pivot']['user_id']);
    return $attributes;
}

This way you will get only attribute required.

Tarunn
  • 1,038
  • 3
  • 23
  • 45
0

You can add it to your "hidden" array. At Model page

protected $hidden = [
    'pivot'
];
iHabboush
  • 537
  • 4
  • 8
0

As mentioned above you can remove the pivot attribute from the response, by adding the following to the related model.

    protected $hidden = [
        'pivot'
    ];

Moreover, in case you want to select specific fields from the pivot to be displayed in the related user object you can add this to your controller using Laravel 5.8. This works also when you hide the pivot information with the above code snippet.

public function index(Trip $trip)
{
    return $trip->users()->select(['trip_id'])->paginate();
}

and you will receive something objects where the trip_id is added to the user object.

    {
        "data": [
         {
                "id": 140,
                "trip_id": 200,
                "name_first": "hasan",
                "name_last": "hasibul",
                "profile_image": "/assets/images/default-profile-img.png",
                "created_at": "2013-09-18 08:19:50",
                "last_login": "2013-12-26 11:28:44",
                "status": "active",
                "last_update": "2013-10-15 13:40:47",
                "google_refresh_token": null,
                "is_admin": 1,
                "updated_at": null,
                "pivot": {
                    "trip_id": 200,
                    "user_id": 140
                }
            }
        ]
    }
Johanna D
  • 21
  • 2