laravel-api-resources-and-pagination

📁 noartem/laravel-vue-skills 📅 Jan 25, 2026
10
总安装量
4
周安装量
#30047
全站排名
安装命令
npx skills add https://github.com/noartem/laravel-vue-skills --skill laravel-api-resources-and-pagination

Agent 安装分布

codex 4
opencode 3
gemini-cli 3
antigravity 2
amp 2

Skill 文档

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