I recently saw a on Laravel Brasil community that turned out to be a lot more interesting than it looks. Imagine you have a with the following implementation: question UsersResource For some reason you might want to reuse that resource class on another endpoint, but hide the email field. This article is an approach on how to achieve that. If you have no idea what API Resources are, check out my previous articles on this subject. First Impression on API Resources API Resources with Nested Relationship 1- Setting up the project The interesting stuff start at section 3. composer create-project --prefer-dist laravel/laravel api-fieldscd api-fieldstouch database/database.sqlite Edit your file to remove database settings and use SQLite .env DB_CONNECTION=sqlite Continue setting up the project php artisan migratephp artisan make:resource UsersResourcephp artisan make:resource --collection UsersResourceCollectionphp artisan make:controller UsersControllerphp artisan tinkerfactory(App\User::class)->times(20)->create();quit 2- The Routes Make sure to create a route in the file. api.php Route:: ('/users', 'UsersController'); apiResource 3- The Controller The Controller represents the desired goal. In this example let’s suppose in the listing we only want the name of all users whereas in the show we want to hide only the email address. In order to achieve this, we need both our and our to know how to handle the call. UsersResourceCollection UsersResource hide 4- The UsersResource Class Let’s start with the method. The will return an object of . As such, we should expose a method that stores the desired keys to be removed from the response. show UsersResource::make UsersResource hide Done! At this point we should be able to access and se a response without the field. http://api.dev/api/users/1 id {"data": {"name": "Mr. Frederik Morar","email": " "}} darryl.wilkinson@example.org 5- The UsersResourceCollection Class For a collection of items to work on the method, we need to perform a few changes: index (1) Make sure returns an instance of UsersResource::collection UsersResourceCollection (2) Expose the method on hide UsersResourceCollection (3) Pass through the hidden fields to UsersResource For (1), we just need to override the method on collection UsersResource For (2) and (3) we need to change the file. Let’s expose the method and process the collection with the hidden fields. UsersResourceCollection hide And that’s it! Now if we call we can see a response without and fields like the specified. http://api.dev/api/users id email UsersController {"data": [{"name": "Mr. Frederik Morar"}, {"name": "Angel Daniel"}, {"name": "Brianne Mueller"}],"links": {"first": " ","last": " ","prev": null,"next": " "},"meta": {"current_page": 1,"from": 1,"last_page": 7,"path": " ","per_page": 3,"to": 3,"total": 20}} http://lab.php71/api-fields-2/public/api/users?page=1 http://lab.php71/api-fields-2/public/api/users?page=7 http://lab.php71/api-fields-2/public/api/users?page=2 http://api-fields.lab.php71/api/users 6- Conclusion The goal was to make the class a little flexible by allowing it to hide some fields that another endpoint may expose. An actual example of this implementation would be a endpoint not including the attribute, but when requesting a specific user via we may desire to include the in the response. Resource /users avatar /users/99 avatar I wouldn’t recommend reusing API Resources too much as it may easily increase the complexity of a layer that is suppose to be simple. With that said, hiding some specific fields between requests of list and specific record does seem a reasonable request on the account of the simplicity of the implementation. 7- What’s Next On a next article, I’ll try to extract the method into a trait and make it reusable throughout all of the API Resources. Follow me on Medium to stay tuned! hide