# Laravel API Resources And Pagination

> Use API Resources with pagination and conditional fields; keep response shapes stable and cache-friendly

- Skill: `noartem/laravel-api-resources-and-pagination` (Agent Skill)
- Install (CLI): `npx skillmds add noartem/laravel-api-resources-and-pagination`
- Raw SKILL.md: https://api.skillmd.com/api/skills/noartem/laravel-api-resources-and-pagination/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Integrations & APIs
- Author: noartem (https://skillmd.com/u/noartem)
- Updated: 2026-09-09
- Page: https://skillmd.com/skills/noartem/laravel-api-resources-and-pagination

---


# 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

