Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

matz's avatar
Level 14

API Resource with pagination

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.

0 likes
8 replies
matz's avatar
Level 14

Nope, without wrapping does not help.

    UserCollection::withoutWrapping();
    return new UserCollection(User::paginate());

AppServiceProvider

/**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        JsonResource::withoutWrapping(); // tried both
        UserCollection::withoutWrapping(); 
    }

I don't have disabled this in my other application. Very strange.

matz's avatar
Level 14

Sorry, cannot undo best answer, clicked wrong.

Yes, I have still

{

    "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"
            }
        ]
    },

as JSON 🙄

usaandi's avatar

@matz Cloned your project and tested. You had to return only your collection without any extra wrapping

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,  // Replaced this with
        	$this->collection, // This
        ];
    }
}


frankielee's avatar

Weird, I have no issue at first, the issue only comes after I adding the code below.


ResourceCollection::withoutWrapping();

1 like
matz's avatar
Level 14

@frankielee now it's freaking. I changed from PHP 7.4.10 back to 7.3 and back to 7.4.10 (I use valet) and it seems to work now Ôo

And it's a bit annoying why

JsonResource::withoutWrapping();; trigger this behavior.

Please or to participate in this conversation.