Skip to main content
A data object will automatically be transformed to a JSON response when returned in a controller:
The JSON response:

Collections

Returning a data collection from the controller:
Will return a collection automatically transformed to JSON:

Paginators

It is also possible to provide a paginator:
The data object is smart enough to create a paginated response with links to the next, previous, last pages:

Transforming Empty Objects

When creating a new model, you probably want to provide a blueprint to the frontend with the required data to create a model:
Instead of making each property nullable, you can return an empty representation:

Default Values

You can provide default values in the constructor:
Or pass defaults within the empty call:
Filter properties in the empty response:

Response Status Code

When a resource is returned from a controller, the status code will automatically be set to 201 CREATED when Laravel Data detects that the request’s method is POST. In all other cases, 200 OK will be returned.

Resource Classes

To make it clearer that a data object is a resource, you can use the Resource class instead of the Data class:
Resource classes won’t validate data or check authorization. They are only used to transform data which makes them faster.