Skip to main content
By default, when a data object is transformed into JSON in your controller, it looks like this:
You can wrap a data object:
Now the JSON looks like this:
Data objects and collections will only get wrapped when sending them as a response, never when calling toArray or toJson.

Default Wrap Key

Define a default wrap key inside a data object:

Global Wrap Key

Set a global wrap key in config/data.php:

Wrapping Collections

Collections can be wrapped just like data objects:
For now, wrapping is only supported for DataCollections, PaginatedDataCollections, and CursorPaginatedDataCollections on the root level. Laravel Collections and arrays cannot be wrapped (yet), though nested properties with such types can be wrapped.
The JSON output:

Paginated Collections

Set the data key in paginated collections:
JSON output:

Nested Wrapping

A data object included inside another data object will never be wrapped even if a wrap is set:
JSON output:

Data Collections Inside Data Objects

A data collection inside a data object will get wrapped when a wrapping key is set (mimicking Laravel resources):
JSON output:

Disabling Wrapping

When a data object is wrapped due to the default wrap method or a global wrap key, you can disable wrapping:

Getting a Wrapped Array

By default, toArray and toJson never wrap a data object or collection. Get a wrapped array manually: