Open danilopinotti opened 3 years ago
Sometimes I need to filter which fields my API will return, but, I'm using JSON Resources and I don't find any 'short way' to filter these fields.
What do you think about the "only" (or/and "except") method in JsonResource?
Example:
use Illuminate\Http\Resources\Json\JsonResource; class UserResource extends JsonResource { /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array */ public function toArray($request) { return [ 'name' => $this->name, 'email' => $this->email, ] } }
class UserController { public function show(User $user) { $fields = request('fields', ['name']); return UserResource::only($fields)->make($user) }
Sometimes I need to filter which fields my API will return, but, I'm using JSON Resources and I don't find any 'short way' to filter these fields.
What do you think about the "only" (or/and "except") method in JsonResource?
Example: