I'm in a blog project. I have this API Resource for my Post model
return [
'id' => $this->id,
'title' => $this->title,
'body' => $this->body,
'date' => $this->date
];
but I don't want to get 'body' => $this->body when I get collection of Posts, because I only use it when I want to show the post, not for listing them
How can I do that ? should I use Resource Collections ?
UPDATE:
makeHidden should work, but it doesn't because we have Illuminate\Support\Collection instead of Illuminate\Database\Eloquent\Collection, how can I make a cast or make API resource's collection method to return an Illuminate\Database\Eloquent\Collection instance ?
I assume you have a
PostResource, if you don't you can generate one:Override the collection method on
PostResourceand filter fields:You need to create a
PostResourceCollectionHere the collection is being processed with the hidden field(s)
Now in
PostControlleryou can call thehidemethod with the field to be hidden:You should get a collection of Posts without the body field.