If you add the data key yourself, you need to disable wrapping with withoutWrapping
https://laravel.com/docs/8.x/eloquent-resources#data-wrapping
Check your old application. You most likely have it in there already
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, I'm a little bit confused after upgrading to Laravel 8.
With my upgraded application, no change. So yesterday I created a fresh application and used API Resource like always.
class UserCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => $this->collection
];
}
}
Sample call
return new UserCollection(User::paginate());
But this time, the data key is double wrapped.
JSON Response looks like
{
"data": {
"data": [
{
"id": 1,
"username": "heavy user",
"email": "[email protected]",
"email_verified_at": "2020-09-24T08:19:16.000000Z",
"profile_photo_path": null,
"created_at": "2020-09-24T08:19:12.000000Z",
"updated_at": "2020-09-24T08:19:16.000000Z"
}
]
},
"links": ...
}
Has anyone an idea about that? I feel a bit stupid, read the docs many times and double checked all :-D Thanks.
Please or to participate in this conversation.