laravel:api-resources-and-pagination

Create paginated Laravel API responses with Resource::collection and conditional fields.

Updated Mar 30, 2026
One-click install
npx skills add https://github.com/Patkik/Multi-tenant-SaaS-Catering-V2 --skill laravel-api-resources-and-pagination-patkik
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: laravel:api-resources-and-pagination
Source: https://github.com/Patkik/Multi-tenant-SaaS-Catering-V2/tree/main/.agents/skills/api-resources-and-pagination
Command: npx skills add https://github.com/Patkik/Multi-tenant-SaaS-Catering-V2 --skill laravel-api-resources-and-pagination-patkik

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Efficiently deliver stable, paginated API responses by leveraging Laravel API Resources rather than raw model data.

Core Features & Use Cases

  • Use Resource::collection with paginate to maintain consistent response shapes across endpoints.
  • Employ when() / mergeWhen() for conditional fields to tailor outputs without breaking contracts.
  • Version resources when contracts change to support backward compatibility and safe evolution.

Quick Start

Create a Laravel Resource and return a paginated collection in a controller to ensure stable, versioned API responses.

Frequently Asked Questions about laravel:api-resources-and-pagination

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I return paginated API resources in Laravel without breaking client contracts?

Laravel API Resources wrap raw model data in a consistent response shape using Resource::collection with paginate. This approach maintains stable contracts across RESTful index endpoints while handling nested relations and conditional fields efficiently.

What is the best way to include conditional fields in Laravel API responses?

The best way to include conditional fields in Laravel API responses is using the when() and mergeWhen() methods within your API Resource classes. These methods conditionally add attributes to the response without breaking existing client contracts.

How do I version Laravel API Resources when response contracts change?

To version Laravel API Resources when contracts change, create separate resource classes for each API version. This supports backward compatibility and safe evolution by mapping raw model data to version-specific response shapes.

Does Laravel API Resources handle nested relations for index endpoints?

Yes, Laravel API Resources handle nested relations for index endpoints. By leveraging Resource::collection with paginate, you can efficiently transform related models into stable JSON structures while maintaining consistent response shapes.

Why should I use Laravel API Resources instead of returning raw model data?

You should use Laravel API Resources instead of raw model data to stabilize API responses. Resources provide a dedicated transformation layer that prevents accidental attribute exposure, manages conditional fields, and ensures consistent pagination metadata.