Laravel - API Resources - Data Wrapping

By default, your outermost resource is wrapped in a data key when the resource response is converted to JSON. So, for example, a typical resource collection response looks like the following:

    
    {
        "data": [
            {
                "id": 1,
                "name": "Eladio Schroeder Sr.",
                "email": "[email protected]",
            },
            {
                "id": 2,
                "name": "Liliana Mayert",
                "email": "[email protected]",
            }
        ]
    }
	

If you would like to use a custom key instead of data, you may define a $wrap attribute on the resource class:

    
    

If you would like to disable the wrapping of the outermost resource, you should invoke the withoutWrapping method on the base Illuminate\Http\Resources\Json\JsonResource class. Typically, you should call this method from your AppServiceProvider or another service provider that is loaded on every request to your application:

    
    
The withoutWrapping method only affects the outermost response and will not remove data keys that you manually add to your own resource collections.

© 2020-2021 | Powered by shifu