Reputation: 1074
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?
Upvotes: 3
Views: 248
Reputation: 9161
I have not tried, but from the laravel docs you can use makeHidden() to temporary hide attributes (relations included), i.e.;
return $model->makeHidden(['someRelation', 'otherRelation'])->toJson();
// or
return $model->makeHidden('someRelation')->makeHidden('otherRelation')->toJson();
Upvotes: 1