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

automica's avatar

Changing links and meta in api response

I'm building a series of endpoints that we want to validate to json:api spec.

In the controller i am using the following:

    public function __invoke(): AnonymousResourceCollection
    {
        return ClientBillingSetupResource::collection($this->provider->getData());
    }

The response from here returns a link and a meta object.

{
  "links": {
    "first": "http://local.site/v2/clients/1/inventory?page=1",
    "last": "http://local.site/v2/clients/1/inventory?page=1",
    "prev": null,
    "next": null
  },
  "meta": {
    "current_page": 1,
    "from": 1,
    "last_page": 1,
    "links": [
      {
        "url": null,
        "label": "pagination.previous",
        "active": false
      },
      {
        "url": "http://local.site/v2/clients/1/inventory?page=1",
        "label": "1",
        "active": true
      },
      {
        "url": null,
        "label": "pagination.next",
        "active": false
      }
    ],
    "path": "http://local.site/v2/clients/1/inventory",
    "per_page": 50,
    "to": 10,
    "total": 10
  }
}

what i would like to do is change the 'total' key to 'totalPages' and add an additional key for self within the links object.

What would be the process for overriding standard laravel pagination object?

0 likes
0 replies

Please or to participate in this conversation.