Making pagination meta fields in Laravel snake case

Viewed 200

I would like to transform Laravel's pagination response meta field into camelCase globally to the application.

From

{
  "data": [
    ...
  ],
  "links": {
    "first": "http://localhost:8081/todo?page=1",
    "last": "http://localhost:8081/todo?page=2",
    "prev": null,
    "next": "http://localhost:8081/todo?page=2"
  },
  "meta": {
    "current_page": 1,
    "last_page": 2,
    ...
  }
}

to

{
  "data": [
    ...
  ],
  "links": {
    "first": "http://localhost:8081/todo?page=1",
    "last": "http://localhost:8081/todo?page=2",
    "prev": null,
    "next": "http://localhost:8081/todo?page=2"
  },
  "meta": {
    "currentCage": 1,
    "lastPage": 2,
    ...
  }
}

I'm using Laravel Resources so I know I can do for each resource something like:

public function toArray($request)
{
    return [
        'data' => $this->collection,
        'meta' => [
            'currentPage' => $this->currentPage(),
            'perPage' => $this->perPage(),
        ],
    ];
}

but this would required to either:

  1. Create a Trait to provide this conversion from snake_case to camelCase
  2. Have to defined in every ResourceCollection this conversion

Is there a global way to modify this response being returned when we are paginating a resource?

0 Answers
Related