Skip to content
Advertisement

What is the difference between a JsonResource & ResourceCollection? in Laravel v6 or v7

Can someone explain the difference between a ResourceCollection and JsonResource?

In Laravel 6 docs you can generate 2 different types of resources… ResourceCollection and JsonResource. https://laravel.com/docs/6.x/eloquent-resources#resource-responses

JavaScript

vs …

JavaScript

Advertisement

Answer

ResourceCollection use for a list of items but JsonResource use for a single item.

I copy piece of code that used it in my project: in my project I had list of articles, so I have to showed list of articles, so I used ResourceCollection that return a array of articles :

JavaScript

this code return for I a list of articles. then when user click on a article, I must show to user a single article, so I must return a single article and I used following code:

JavaScript

you can see the this my answer that related to your question

I hope you find it useful.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement