Skip to main content
API Resources provide a transformation layer between your Eloquent models and the JSON responses sent to your API clients. They give you fine-grained control over which attributes are exposed and how they’re formatted.

Basic Resource

Create a resource by extending ApiResource:
The $this->resource property contains the underlying model instance passed to the resource constructor.

Resource Collections

Transform multiple models at once:

Collection Output

Conditional Attributes

Include attributes based on conditions:

Relationships

Include related models in your resource:

Meta Data

Add metadata to responses:

Output with Meta

API Response Helpers

Use ApiResponse for standardized JSON responses:

Success Response

Error Response

Response Types

Paginated Responses

Handle pagination elegantly:

Paginated Output

API Controller

Use ApiController as a base for your API endpoints:
The ApiController provides helper methods for common API responses: success(), error(), notFound(), unauthorized(), forbidden(), validationError(), created(), and deleted().

Advanced Resource Patterns

Computed Attributes

Nested Resources

Resource Wrapping

Real-World Example

Complete API endpoint with resources:

Performance Tips

Prevent N+1 queries by eager loading relationships:
Allow clients to request specific fields:

Next Steps

API Router

Learn about API routing

Admin Dashboard

Build admin panels