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

giagara's avatar

Response marco with api resource and model paginate

Description:

When exposing data trough response macro with an API resource collection with a pagination data, the json response is not containing the pagination's informations.

Steps To Reproduce:

Link to a repository: github.com/giagara/marco-apiresource-bug

  1. Add response macro to AppServiceProvider
Response::macro('success', function ($data) {
    return response()->json([
        'status' => 1,
        'content' => $data
    ]);
});
  1. Create a UserResource (collection) php artisan make:resource UserResource -c
public function toArray($request)
{
    return parent::toArray($request);
}
  1. Call the resource in the macro with pagination (in the route for instance):
Route::get('/', function () {
    return response()->success(new UserResource(User::paginate()));
});

Expected result:

{
    "status": 1,
    "content": {
        "data": [
            {
                "id": 1,
                "name": "Alan Bartell",
                "email": "[email protected]",
                "email_verified_at": "2021-10-04T07:01:08.000000Z",
                "created_at": "2021-10-04T07:01:08.000000Z",
                "updated_at": "2021-10-04T07:01:08.000000Z"
            },
            {
                "id": 2,
                "name": "Dr. Karlee Little",
                "email": "[email protected]",
                "email_verified_at": "2021-10-04T07:01:08.000000Z",
                "created_at": "2021-10-04T07:01:08.000000Z",
                "updated_at": "2021-10-04T07:01:08.000000Z"
            },
            ...
        ],
        "links": {
            "first": "http://127.0.0.1:8000?page=1",
            "last": "http://127.0.0.1:8000?page=7",
            "prev": null,
            "next": "http://127.0.0.1:8000?page=2"
        },
        "meta": {
            "current_page": 1,
            "from": 1,
            "last_page": 7,
            "links": [
                {
                    "url": null,
                    "label": "« Previous",
                    "active": false
                },
                ...
                
            ],
            "path": "http://127.0.0.1:8000",
            "per_page": 15,
            "to": 15,
            "total": 100
        }
    }
}

Actualresult:

{
    "status": 1,
    "content": [
        {
            "id": 1,
            "name": "Alan Bartell",
            "email": "[email protected]",
            "email_verified_at": "2021-10-04T07:01:08.000000Z",
            "created_at": "2021-10-04T07:01:08.000000Z",
            "updated_at": "2021-10-04T07:01:08.000000Z"
        },
        {
            "id": 2,
            "name": "Dr. Karlee Little",
            "email": "[email protected]",
            "email_verified_at": "2021-10-04T07:01:08.000000Z",
            "created_at": "2021-10-04T07:01:08.000000Z",
            "updated_at": "2021-10-04T07:01:08.000000Z"
        },
        ...
    ]
}
0 likes
4 replies
martinbean's avatar

@giagara Just return the resources from your controller. You don’t need that macro. You also don’t need to include a success key in response bodies; that’s what HTTP status codes are for.

1 like
giagara's avatar

@martinbean thanks, but for the purpose of my application i need that flag. I am interacting with a mobile application that i cannot refactor/rebuild

martinbean's avatar

@giagara Clients should not be dictating what responses an API returns. There should be absolutely no reason you “have” to structure your response that way because of a mobile app. And if they made the assumption before the API was built, then you should tell them that no, that’s not what the API returns.

I can’t go to Stripe and say, “Yo, nice API an’ all, but my mobile app is expecting X in responses. Can you change your API, please? kthxbai”

If you’re providing an API then you’re the vendor. Things like mobile apps are just consumers. They don’t get to tell you how to structure things in your API.

1 like
giagara's avatar

@martinbean I am so sorry about it, but there is any client and, still, i HAVE to maintain this structure. It's not my decision, it's just what I have to do. I understand and agree on what you are saying, but I am seeing any help about it on my question and on the error i get.

For the purpose of the error I can change the macro, adding some random data, and you will concentrate on the real question?

If you want to help I will appreciate it, in any other way please, don't judge my requirements.

Thanks

Please or to participate in this conversation.