Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SteveBelanger's avatar

Resource vs ResourceCollection

After watching the video What’s new in Laravel 5.5 - API resources (from 06:40 to 09:20 is the relevant part) , and since I have not dealt with resources and restful API’s before, I have this question :

There is this Resource Class which acts like a transformer to take what’s in the model and make it conform to what should be the response to a request. Then, there is ResourceCollection, which deals with collections of resources, and adds pagination and other nice things to handle collection. And my question is this :

Why would I ever use Resource, if ResourceCollection does everything Resource does, and more ? 1 resource can be considered a collection of 1 recource... right ?

P.S.: My main question is the above, with a question mark. But I did try to not be lazy and look at the classes. I found that Resource has a method called collection, which returns an instance of AnonymousResourceCollection which extends ResourceCollection, while ResourceCollection extends Resource. My brain hurts.

0 likes
3 replies
SteveBelanger's avatar

... just pushing my question up, once. I will not abuse of it.

sutherland's avatar
Level 28

I believe ResourceCollection is not really meant to transform the models themselves, but to include the metadata like pagination links, collection size, etc. And you don't usually need to include that metadata on an endpoint where you only ever expect one result.

And what about when your models have relationships? Say you have a movie and it has a director you want to include as a child property. Well, you don't need pagination on the director property and instead of writing the logic to transform the director in the MovieResource, you can write a DirectorResource that can be reused across different endpoints.

3 likes
SteveBelanger's avatar

@sutherland

Fair enough. Thank you for your answer. With some tinkering and some real usecase to handle, I will get it.

1 like

Please or to participate in this conversation.