Eloquent: API Resources

Tram Ho

1. Preface

The theoretical point of view is that the minority only practices to make you understand, so I will write an honest, easy-to-understand article with lots of examples for you to make subjective assessments. about seemingly vague concepts ? . Ok let’s get started

Surely people are not too far away with the term API Resources, or some of you are learning some more keywords to fit the requirements of the business. Do not worry, in this article I will together with you to learn about it, what it is used for, what it does, how it is applied, for each specific case I will try to Take an example for you to analyze according to your own subjective opinion ?

2. What is API Resources:

When building the API, you may need a conversion class between your Eloquent models and JSON responses to return your application users. Laravel’s resource classes allow you to easily convert models and model collections into JSON.

Or can understand simple. For example, you go online to find any api to make your web focus on the paragraph:

If you’ve ever looked through the front end. Then I do not care what language you use or what server processing, I will only care about specific cases how you will return data. And vice versa, I will not care what language you use to code the interface at all, I only care about each specific case I will return a piece of data. This will separate the front end and back end for code and project functionality.

3. Create API Resources

To use resources first we need to create a model, resource, and a method in our controller for our table with the command. Here I assume you already have it (If you do not have it, you can use the command

to quickly create your controller, model and factory files) here I will give an example with my Product model ? . Now I will create resource Product

Remember to pay attention to the way you named it to follow the format correctly ? . The above statement will create a ProductResource class in app / Http / Resources, which will have the following content:

4. How to use it

By default, resources will be available in the resource toArray method. You can customize it to fit your program as follows

And I will create a link route so when you call will return the data on the following output:

Inside the index method of the ProductController file I will write the following:

Ok, come here as the code Now instead of continuing to take some extended functions, I will analyze and go and will share about the flow of the code above. ? .

First, if you are using the old way of returning a view, you will see the paragraph

In fact, it is not much different, instead of passing variables to let us write the interface, now we will return an API standard for convenient exchange and proper standard only.

For those of you who don’t know, compile the code here: Call a resoure ResoureProducts and pass an array of values ​​to it Product :: with ([‘event’]) -> get () you notice ‘event ‘ This is the model name. Oh forget that these ResoureProducts are resoure created by you above ? . his is

As for the route file, you all know it ?

Next at ResoureProducts , you use $ this to get the value set in your data set. Can you see that I took the id, name … out, right? ? . In the special case here through the ‘events’ link between the 2 tables, I want to get some information about this ‘events’ . To get the information I need at the event, I also customize this event via the ResourceEvent file. Here in relation to an event with many products (Or create something), I write

You are wondering if this is a collection, an array of data (Or take out something), how to retrieve it, right? actually very simple you just call

With whenLoaded (), it can be understood simply that it will check if there exists a relationship between these two tables or not, if any, to perform.

5. Expand

The article comes here is the end ? , but I will share some more functions I feel are quite good for some of you want to learn more ? .

First, if you notice, your JSON string will be wrapped by a data [] like this, which will sometimes make you feel uncomfortable when mapping in your program when everything is You must declare data [] -> which you need . Laravel has given you a great way to get rid of it, where you just need to map -> a new horizon ? . You just need to go to the AppServiceProvider file with the link app / Providers / AppServiceProvider and add

Go to your boot () function and run. Simple is not it.

You can also consider a field that satisfies a certain new condition for it to display, usually by checking the right with the following command.

Compile the following statement: If it satisfies the isAdmin () function, it will return the secret-value, you can also use a ‘Closure’ to replace the secret-value.

Next is the setpivot function, if true, it will return a closore

As the name of the function, we can understand this function will retrieve data in the pivot table to help us custorm the information that the extra table returns.

6. Conclusion

By reading this, you probably have an idea of api resours and what are some basic functions in api resours and how to use them correctly. Thank you for reading my article, if you have any questions, or if you have any part that you do not understand, you can comment below the comment section so that we can learn better. And especially do not forget to give me a like and a share ?!

Share the news now

Source : Viblo