Skillshub laravel-api-resources-and-pagination

API Resources and Pagination

install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/noartem/skills/laravel-api-resources-and-pagination" ~/.claude/skills/comeonoliver-skillshub-laravel-api-resources-and-pagination && rm -rf "$T"
manifest: skills/noartem/skills/laravel-api-resources-and-pagination/SKILL.md
source content

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