Resource Collections & Pagination
Resource Collections wrap paginated results with consistent metadata - pagination info, links, and additional data. Laravel automatically includes pagination meta when you pass a paginator to a resource collection.
Resource Collections
// php artisan make:resource PostCollection
class PostCollection extends ResourceCollection
{
public function toArray(Request $request): array
{
return [
'data' => $this->collection, // Uses PostResource automatically
'meta' => [
'total_posts' => Post::count(),
'published_posts' => Post::published()->count(),
],
];
}
}
// Controller usage
public function index()
{
$posts = Post::published()->paginate(15);
return new PostCollection($posts);
}
// JSON output includes:
// {
// "data": [ ... post resources ... ],
// "meta": {
// "total_posts": 150,
// "published_posts": 120,
// "current_page": 1,
// "last_page": 10,
// "per_page": 15,
// "total": 120
// },
// "links": {
// "first": "http://app.com/api/posts?page=1",
// "last": "http://app.com/api/posts?page=10",
// "next": "http://app.com/api/posts?page=2"
// }
// }Industry Best Practices
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class
- Profile performance with Laravel Debugbar (composer require barryvdh/laravel-debugbar) during development - it shows query counts, memory usage, and timeline for every request
- Keep dependencies up to date: run composer audit regularly to check for security vulnerabilities in your package dependencies
Industry Best Practices
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class
- Profile performance with Laravel Debugbar (composer require barryvdh/laravel-debugbar) during development - it shows query counts, memory usage, and timeline for every request
- Keep dependencies up to date: run composer audit regularly to check for security vulnerabilities in your package dependencies
Industry Best Practices
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class
- Profile performance with Laravel Debugbar (composer require barryvdh/laravel-debugbar) during development - it shows query counts, memory usage, and timeline for every request
- Keep dependencies up to date: run composer audit regularly to check for security vulnerabilities in your package dependencies
Tip
Tip
Practice Resource Collections Pagination in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
LazyCollection for large data.
Practice Task
Note
Practice Task - (1) Write a working example of Resource Collections Pagination from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Resource Collections Pagination is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready laravel code.
Key Takeaways
- Resource Collections wrap paginated results with consistent metadata - pagination info, links, and additional data.
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class