as far as i understand, resources are a mechanism to represent models, in other words transform model data for json api response, and by doing that we can take away code from controllers and make controllers more clear
Yep, you got it, also it makes this reusable code, so you can use it in many parts of a controller using the eager load methods presented in the first example
Thank you for this video. On my point, resources very helpfull feature. I use them all time during development application, becouse it helps me keep my code cleanest and clearer. And, of course, resources help me more flexible creating response formats
If returning data with a laravel api takes 637 ms the same size of data in lumen it takes 150 ms . so i prefer to do it with lumen i hope if there could be a way to remove all the extra features from laravel in a api response to increase the speed.
in some case, when we only need to transform some data in one place, we can do without creating another api resource class like this User::paginate(10) ->through(function ($user) { return [ 'id' => $user->id, 'name' => $user->name, 'email' => $user->email, 'image' => $user->image ]; });
@@raimonds.L its personal preferences, sometimes i just dont like many class when apps get bigger, and have many class that only used 1 time its just not really necessary
Resource was not well thought out .... if you do c# net core precisely... you will know that a simple return class will do all these bulky things faster and more cleaner
I was using this approach today and I found something strange related this topic and response macro (another of your video). If you use api resource (collection) with pagination and response macro, the pagination data, like links, desappear from the response content. Can you reproduce this bug?
Hello Mr.Povilas, I have a minor question if you may... Is it a good practice to shuffle or reconstruct an array using resources API? And if no what do you recommend other than some complex algorithms?
Not sure what exactly you mean by complex algorigthms, need examples. But probably, complex algorithms like require complex solutions, more than default API resources.
@@LaravelDaily well it might not be complex, just reorganizing the array to another form or combining between two arrays in a specific order. Knowing that it is just a regular controller that will pass the data to a view and not an API response. So is using resources a good practice or should we for example use collections
I'm looking for solution.. how can we search database on the basis of relationship.. like i have a situation.. a field technology and other is version.. both has different tables and the pivot.. i want to search a technology with specific version if exists in relation
I just feel using resource is a long thing... it's better you create a return class and map whatever you need to send to the api from the parents. ... this is one of the reason c# net core api is faster. Using resource is bulky
In my initial projects I did not use it because it was supposed to hard for me but when I decided to learn and implement, it was more simpler then I thought.
thanks for great videos, Mr. Korop. i would like you kindly ask you, if you have a time, to teach laravel function. When I read code of my company Laravel Project, one is coding new User , and another is app(User::class) for getting new instance. what is wrong new xxxx and app(xxxx::class) ? what is using properly? if you talk about anything, very happy!