Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pivot attribute with toArray/toJson method #745

Closed
schickling opened this issue Mar 28, 2013 · 12 comments
Closed

Remove pivot attribute with toArray/toJson method #745

schickling opened this issue Mar 28, 2013 · 12 comments

Comments

@schickling
Copy link

Please remove the pivot attribute of a "jsonified" model. Since it makes an api "fatter" then needed.

{
id: 87,
title: "Title",
pivot: {
user_id: 1,
photo_id: 87
}
@taylorotwell
Copy link
Member

Add it to your "hidden" array.

@schickling
Copy link
Author

You're right!

@mauris
Copy link

mauris commented May 13, 2014

For future fellow Laravel 4.1.x journeymen, please refer to the Docs under Eloquent / Converting to Arrays or JSON on how to use the hidden array.

<?php

class User extends Eloquent
{

    protected $hidden = array('pivot');

}

@monove
Copy link

monove commented May 20, 2015

But how do you hide a specific field within pivot table, like

<?php
class User extends Eloquent
{
    protected $hidden = array('pivot.created_at');
}

@vatps
Copy link

vatps commented May 20, 2015

@monove Following trick worked for me...

Add following method to your Model.

    /**
     * Convert the model instance to an array.
     *
     * @return array
     */
    public function toArray()
    {
        $attributes = $this->attributesToArray();
        $attributes = array_merge($attributes, $this->relationsToArray());
        unset($attributes['pivot']['created_at']);
        return $attributes;
    }

@alxlacayo
Copy link

@vatps yes!!! man thank you!!!

@manojn-iprogrammer
Copy link

great!! thank you :)

@timothyallan
Copy link

You can also use that toArray() function to rename your pivot value so it's "inline" with the rest of your data.

i.e.

  public function toArray()
    {
        $attributes = $this->attributesToArray();
        $attributes = array_merge($attributes, $this->relationsToArray());

        // Detect if there is a pivot value and return that as the default value
        if (isset($attributes['pivot']['value'])) {
            $attributes['value'] = $attributes['pivot']['value'];
            unset($attributes['pivot']);
        }
        return $attributes;
    }

@sword-jin
Copy link

Mark, thank you guys

@fdorantesm
Copy link

@taylorotwell Any way to remove using query builder? ie. I need pivot key in a specific endpoint.

@powelski
Copy link

Adding pivot to $hidden doesn't sound right. There are multiple contexts where pivot property can appear, for different relationships and different use cases. I don't see much value in hiding it from "everywhere".

@epixian
Copy link

epixian commented Mar 15, 2020

Adding pivot to $hidden doesn't sound right. There are multiple contexts where pivot property can appear, for different relationships and different use cases. I don't see much value in hiding it from "everywhere".

you can temporarily hide attributes using makeHidden(), documented since ~5.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests