Monday, April 10, 2017

Hide extra relations that are loaded in an accessor

I'm loading relations inside an accessor attribute:

public function getNameAttribute()
{
    return $this->someRelation->name . ' x ' . $this->otherRelation->name;
}

When ->toJson is called when sending data via Http response, the data from these relationships are included, but I only want to include the name attribute.

One solution could be just setting these relationships to hidden, but I'm reluctant to because I worry that will come back to bite me if I need to actually load those relationships.

How can I avoid this "side effect" data when converting my models to an array/json?



via Dallin

Advertisement