L
laravel-api-resources-and-pagination
About laravel-api-resources-and-pagination
A skill for Laravel developers that covers representing models with API Resources and handling pagination. It emphasizes preserving pagination metadata for clients and managing resource contract changes.
Key features
- 01Return models through Resource collections
- 02Add conditional fields
- 03Preserve pagination links and cursors
- 04Version resource contracts
Use cases
- 01Build paginated API responses
- 02Return fields conditionally
- 03Evolve API resource contracts
Install
Copy the command and run it in a terminal.
$
npx skills add https:/ / github.com/ noartem/ skills --skill laravel-api-resources-and-paginationAfter installing, type/laravel-api-resources-and-paginationin Claude Code to confirm it loaded.
Details
From the project READMEAPI Resources and Pagination
Represent models via Resources; keep transport concerns out of Eloquent.
Commands
# Resource
php artisan make:resource PostResource # or: php artisan make:resource PostResource
# Controller usage
return PostResource::collection(
Post::with('author')->latest()->paginate(20)
);
# Resource class
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'author' => new UserResource($this->whenLoaded('author')),
'published_at' => optional($this->published_at)->toAtomString(),
];
}
Patterns
- Prefer
Resource::collection($query->paginate())over manual arrays - Use
when()/mergeWhen()for conditional fields - Keep pagination cursors/links intact for clients
- Version resources when contracts change; avoid breaking fields silently
Comments
Markdown supported