-
Lets say I have a I tried solving this by creating a custom public function toArray(Request $request): array
{
return [
'data' => $this->collection->map(function(ClientResource $resource) use ($request) {
return Arr::only($resource->toArray($request), ['id', 'username', 'email']);
}
];
} This works fine until I try to include entries that could be Since I'm not working that long with API Resources I'm wondering if there is a better way to solve my usecase or if this is a bug. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can call to inheritance. You have a public function toArray(Request $request): array
{
return Arr::only(parent::toArray(request), ['id', 'username', 'email']));
} And instead of public $collects = BriefUserResource::class; No need to override Next question is why do you even need to populate |
Beta Was this translation helpful? Give feedback.
You can call to inheritance.
You have a
UserResource
which is a full resource. Create a childBriefUserResource
with overriddentoArray()
as (fill free to make a trait):And instead of
UserResource
point yourResourceCollection
toBriefUserResource
:No need to override
toArray()
in your collection.Next question is why do you even need to populate
street
,city
,country
,zipcode
, if they wouldn't be needed? And reverse the inheritance.