Api resources and pagination

From jpcaparas/superpowers-laravel · View interactive version

Api resources and pagination is an AI agent skill from the GitHub repository jpcaparas/superpowers-laravel. Use API Resources with pagination and conditional fields; keep response shapes stable and cache-friendly

How to use this skill

Skill file

API Resources and Pagination

Represent models via Resources; keep transport concerns out of Eloquent.

Commands

# Resource
sail 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

Laravel 13+: JSON:API Resources

First-party JSON:API resources handle resource objects (type/id/attributes/relationships), ?include= relationship inclusion, ?fields[...]= sparse fieldsets, links, and application/vnd.api+json headers.

class PostJsonApiResource extends JsonApiResource
{
    public function toArray($request)
    {
        return [
            'type' => 'posts',
            'id' => $this->id,
            'attributes' => ['title' => $this->title, 'body' => $this->body],
            'relationships' => ['author' => new UserJsonApiResource($this->author)],
        ];
    }
}
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.