What is API Resource in Laravel ?
Laravel is an awesome and most popular Php Framework. Laravel has a strong substructure and it let us create strong projects.
We will discuss API Resource in this article. API is a door for other applications and users. You get a request from API and you respond as a result of this request. When you respond to any request, You may need to protect some data of the model’s content. You can write a function to that but it’s an unuseful way for that solution because you may need more than 1 time for that function and so you need to describe it as global or static. That’s not a modern way. Api Resource lets us solve that problem as easily because it has a specific structure for that.
How To Create API Resources?
API Resources has a really easy structure and it’s easy to create a resource’s file. You can create it manually or you can create it by Artisan command.
We have a user table and a user model in our project. Let’s say, the user wants to display his profile and we will respond to that user’s detail like name, email, address, age, etc. also the user has rows on the blog table. User and Blog tables are in relation to the Laravel model relationship. If we respond whole detail directly, we will have a problem with security and the response will not be clear. We need a function to protect some columns and also need another function to make a useful table relationship. API Resources are solving that problem and it makes a useful structure for reponse data. Let’s see how it happen.
Firstly, we need to create a User Resource for our User Model.
php artisan make:resource UserResource
Second, we need to create a Blog Resource for our Blog Model.
php artisan make:resource BlogResource
it’s all that we need. After that you can see BlogResource.php and UserResource.php under Resources file.
→ app\Http\Resources
User Model Has:
→ id
→ name
→password
→secret_key
→image
→ created_at
Blog Model Has :
→ id
→ user_id
→ title
→ text
→ photo
→created_at
We need to hide password and secret_key in our response. Let’s see our UserResource’s content :
→ app\Http\Resources\UserResource
As you can see, $this parameter includes User Model’s content and we do not add password and secret_key to our Resource’s content. In this case, User and Blog model has a relationship. API Resource let us use that relationship in the Resource structure. As you can see above code, we used BlogResource for user’s blogs.
Let’s see Blog Resource :
→ app\Http\Resources\BlogResource
We have also described Blog Model’s columns.
Our User Model :
Our Blog Model :
We can use a basic Controller for this example.
In this article, we discussed API Resource in Laravel. I hope it will be a useful article for you and it will help you to develop better projects.
If you have any questions or ideas, you can share them with me! If you would like to give me feedback, I will be so happy about that.
Until the next article;
Enjoy your code !
Resources:
https://laravel.com/docs/8.x/eloquent-resources
Enver ŞANLI
Web Developer, Farmer & Social Thinker
I’m Enver, Middle Laravel Developer.