Loading...
Loading...
Use API Resources with pagination and conditional fields; keep response shapes stable and cache-friendly
npx skill4agent add noartem/skills laravel-api-resources-and-pagination# 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(),
];
}Resource::collection($query->paginate())when()mergeWhen()