Laravel api resources and pagination
Skill ComeOnOliver/skillshub/skills/noartem/skills/laravel-api-resources-and-pagination
🧠The right skill, one API call. AI agent skills registry with token-efficient skill resolution. 5,000+ skills from 500+ top repos.From the repository description
npx -y skills add ComeOnOliver/skillshub --skill laravel-api-resources-and-paginationAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
SKILL.md
0.8 KB, 198 tokens by cl100k_base, as published. Nobody here has run it
API 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
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.