From the course: Building a Website with Laravel, React.js, and Inertia

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Creating Post API resource

Creating Post API resource

- [Instructor] Before we create the controller methods, let's create a post API resource. API resources are templates where we define how we want adjacent data to be returned back to the user when they send the API request. Without a resource, everything that is not hidden would be returned. If we look at the user model, we can see that it has two hidden attributes, password and remember token. These attributes will not be sent in an API response. And we could create our hidden attributes for the post model too, and we would do that for sensitive information, but using API resources is more convenient when we want to transform the data in some way, not just hide it. Let's create the resource by running PHP artisan make resource post resource in the terminal. The current default is to return everything about a post. This line is equivalent to saying return the ID, the author, the title, and the body attributes. But if we only wanted to return the author data, we would remove the title…

Contents