Reputation: 3404
Is it possible to show new added Laravel resources API fields on specific routes?
Example: I have a UserResource
which has 3 fields - id, name and email
. I'm returning UserResource
in 10 routes. For 11th route added new field - posts
.
Is it possible to show new posts
field only for 11th route?
Upvotes: 0
Views: 117
Reputation: 1021
Of course you can, so you need to add two things. First inside of your controller in the method where you want to load posts do the following
return UserResourse::collection($users->with('posts'));
Then inside of your UserResourse
class add the following
'posts' => PostResourse::collection($this->whenLoaded('posts'))
// or like this if you dont have a PostResouse
'posts' => $this->posts
Upvotes: 1