Skip to main content

What are API resources

When building an API, returning Eloquent models directly as JSON can expose unwanted columns or send more data than the client needs. Eloquent API resources place a transformation layer between your models and JSON responses. The toArray() method lets you explicitly define what fields to include and in what format. Key benefits:
  • Full control over which fields appear in the response
  • Field renaming and value formatting in one place
  • Conditionally include or exclude fields
  • Nest related resources for a consistent structure

Generating a resource

Use the make:resource Artisan command to generate a resource class.
The class is placed in app/Http/Resources.
You access model properties directly via $this because the resource proxies property access to the underlying model.

Returning a resource from a controller

Return the resource from a route or controller method.
Or use the model’s toResource() convenience method, which discovers the matching resource class by convention.
By default, the response wraps the data in a data key.

Resource collections

To return multiple models, use the collection() method.
Or use the Eloquent collection’s toResourceCollection() method.

Custom collection resource

When you need to add metadata to the entire collection, generate a dedicated collection resource.

Transforming fields

Rename keys or transform values directly inside toArray().

Conditional fields

when() — include a field based on a condition

Use when() to include a field only when a condition is true. When the condition is false, the key is removed entirely from the response.

mergeWhen() — conditionally merge multiple fields

When several fields share the same condition, use mergeWhen() to add them together.

whenLoaded() — include a relationship only when loaded

Use whenLoaded() to avoid triggering unintended queries. The relationship is included only when it has already been eager-loaded.
The controller decides which relationships to load.

whenCounted() — conditionally include a relationship count

Nested resources

Nest related resources to maintain a consistent response structure across your API.

Adding metadata

with() — top-level metadata on a collection

Override the with() method to add metadata returned only when the resource is the outermost response.

additional() — add metadata dynamically in the controller

Pagination

Pass a paginator to a resource collection and Laravel automatically appends meta and links to the response.
Or use the paginator’s toResourceCollection() method.
Response:
Paginated responses always include the data wrapper, even if you have called withoutWrapping(). This ensures the meta and links keys have a place to live alongside the data.

Disabling data wrapping

By default, the outermost resource is wrapped in a data key. To disable this, call withoutWrapping() in AppServiceProvider.
withoutWrapping() only affects the outermost wrapper. It will not remove any data keys you add manually inside your resource classes.

Practical example: user API

The following example shows a complete user resource with an accompanying controller.

UserResource

UserController

Eloquent relationships

Learn how to define relationships and use eager loading.

Pagination

Combine pagination results with API resources.
Last modified on April 3, 2026