Laravel 5.6: Customise a paginated resource collection meta and links attributes

6.5k views Asked by At

How can I customize Laravel ResourceCollection meta and links information.

Links should include only prev,next and self instead of first,last,prev,next that is by default.

Meta should include pagination iformation like: current_page, total_items, items_per_page, total_pages instead of current_page, from, last_page, path, per_page, to, total.

This is how meta and links information looks now in JSON response:

"meta": {
    "currentPage": 2,
    "current_page": 1,
    "from": 1,
    "last_page": 3,
    "path": "http://localhost:8000/api",
    "per_page": 5,
    "to": 5,
    "total": 14
},
"links": {
    "self": "http://localhost:8000/api",
    "first": "http://localhost:8000/api?page=1",
    "last": "http://localhost:8000/api?page=3",
    "prev": null,
    "next": "http://localhost:8000/api?page=2"
}

.. I want it to be something like:

"meta": {
    "current_page": 1,
    "total_items": 15,
    "per_page": 5,
    "total_pages": 3
},
"links": {
    "prev": null,
    "next": "http://localhost:8000/api?page=2"
    "self": "http://localhost:8000/api",
}
1

There are 1 answers

3
Yahya Uddin On

I have not been a fan with how Laravel has implemented paginators and resources, as its difficult to do certain things like the issue that you mentioned.

Internals

Before you can customise your responses in the way you want, you first need to understand how ResourceCollections are converted to responses.

The original toResponse method for resource collections looks like this:

public function toResponse($request)
{
    return $this->resource instanceof AbstractPaginator
                ? (new PaginatedResourceResponse($this))->toResponse($request)
                : parent::toResponse($request);
}

If you look in further into PaginatedResourceResponse class you will see the following code.

...
protected function paginationLinks($paginated)
{
    return [
        'first' => $paginated['first_page_url'] ?? null,
        'last' => $paginated['last_page_url'] ?? null,
        'prev' => $paginated['prev_page_url'] ?? null,
        'next' => $paginated['next_page_url'] ?? null,
    ];
}
...

protected function meta($paginated)
{
    return Arr::except($paginated, [
        'data', 
        'first_page_url',
        'last_page_url',
        'prev_page_url',
        'next_page_url',
    ]);
}

I recommend reading Illuminate\Http\Resources\Json\PaginatedResourceResponse and Illuminate\Http\Resources\Json\ResourceResponse fully to understand what's going on.

Solution 1: Create a custom PaginatedResourceResponse

One solution is to create a new class that extends PaginatedResourceResponse, and override the paginationLinks method.

So it look something like:

use Illuminate\Http\Resources\Json\PaginatedResourceResponse;

class CustomPaginatedResourceResponse extends PaginatedResourceResponse
{
    protected function paginationLinks($paginated)
    {
        return [
            'prev' => $paginated['prev_page_url'] ?? null,
            'next' => $paginated['next_page_url'] ?? null,
        ];
    }

    protected function meta($paginated)
    {
        $metaData = parent::meta($paginated);
        return [
            'current_page' => $metaData['current_page'] ?? null,
            'total_items' => $metaData['total'] ?? null,
            'per_page' => $metaData['per_page'] ?? null,
            'total_pages' => $metaData['total'] ?? null,
        ];
    }
}

Then you can override your toResponse method to look something like:

public function toResponse($request)
{
    return $this->resource instanceof AbstractPaginator
                ? (new CustomPaginatedResourceResponse($this))->toResponse($request)
                : parent::toResponse($request);
}

You may consider overriding overriding other methods if you want to customise your response further.

Solution 2: Override toResponse in the ResourceCollection

Instead of overriding the PaginatedResourceResponse, you can just override the toResponse method in the ResourceCollection with a lightweight version of similar code like so:

public function toResponse($request)
{
    $data = $this->resolve($request);
    if ($data instanceof Collection) {
        $data = $data->all();
    }

    $paginated = $this->resource->toArray();
    // perform a dd($paginated) to see how $paginated looks like

    $json = array_merge_recursive(
        [
            self::$wrap => $data
        ],
        [
            'links' => [
                'first' => $paginated['first_page_url'] ?? null,
                'last' => $paginated['last_page_url'] ?? null,
                'prev' => $paginated['prev_page_url'] ?? null,
                'next' => $paginated['next_page_url'] ?? null,
            ],
            'meta' => [
                'current_page' => $metaData['current_page'] ?? null,
                'total_items' => $metaData['total'] ?? null,
                'per_page' => $metaData['per_page'] ?? null,
                'total_pages' => $metaData['total'] ?? null,
            ],
        ],
        $this->with($request),
        $this->additional
    );

    $status = $this->resource instanceof Model && $this->resource->wasRecentlyCreated ? 201 : 200;

    return response()->json($json, $status);
}

Solution 3: Override withResponse method

A simpler, but perhaps less powerful option is to just override the withResponse at the resource collection like so:

public function withResponse($request, $response)
{
    $data = $response->getData(true);
    $prev = $data['links']['prev'];
    $next = $data['links']['next'];
    $self = $data['links']['self'];
    $data['links'] = compact('prev', 'next', 'self');
    $response->setData($data);
}