laravel-list-resource-collection maintained by neclimdul
Description
Hack to allow Laravel resource collections to list scalars.
Author
Last update
2026/04/23 04:08
(dev-main)
License
Downloads
163
ListResourceCollection
Summary
When returning a paged results from Laravel, you're given a lot of tools using Resources to interact with Eloquents pager and provide rich, consistent JSON results. However, it assumes all your results are an array or objects. If you want to return a list of scalars, for example a list of IDs, it breaks.
This hacks package provides a Resource collection that hacks this process to allow a list of unprocesses scalars.
There's probably a better way, or maybe Laravel could better support this out of the box, but this solved the problem in the short term.
Example
<?php
$contacts = Contact::query()
->paginate(100, ['uuid']);
$contacts->setCollection($contacts->getCollection()->pluck('uuid'));
return new PaginatedResourceResponse(
ListResourceCollection::collection($contacts)
);