I was wondering if it is possible to define different data for a resource resource and a collection resource.
For the collection I want to send ['id', 'title', 'slug'] , but the resource of the element will contain additional information ['id', 'title', 'slug', 'user', etc.]
I want to achieve something like:
class PageResource extends Resource { public function toArray($request) { return [ 'id' => $this->id, 'title' => $this->title, 'slug' => $this->slug, 'user' => [ 'id' => $this->user->id, 'name' => $this->user->name, 'email' => $this->user->email, ], ]; } } class PageResourceCollection extends ResourceCollection { public function toArray($request) { return [ 'id' => $this->id, 'title' => $this->title, 'slug' => $this->slug, ]; } }
PageResourceCollection will not work properly because it uses PageResource , so it needs
return [ 'data' => $this->collection, ];
I could duplicate the resource in PageFullResource / PageListResource and PageFullResourceCollection / PageListResourceCollection , but I'm trying to find a better way to achieve the same result.
source share