DEV Community

Cover image for Customizing API Resource Pagination Structure
Phyo Thiha
Phyo Thiha

Posted on

3

Customizing API Resource Pagination Structure

We have been in the same boat, trying to figure out customizing the pagination structure that was returned from the resource or resource collection. Records are wrapped in data key with additional links and meta that might be unnecessary or want to modify for a different scenario.

Before Laravel 10, you need extra steps to create base resource class that extends JsonResource, modify the collection method from the JsonResource and stuffs. Check this github discussion to modify the structure.

Starting from Laravel 10, there is a method called paginationInformation that can customize pagination data structure. All you have to do is define the method on the resource collection and modify the structure.

As docs per se

This method will receive the $paginated data and the array of $default information, which is an array containing the links and meta keys:

public function paginationInformation($request, $paginated, $default)
{
    $default['pagination']['current_page'] = $default['meta']['current_page'];
    $default['pagination']['last_page'] = $default['meta']['last_page'];
    $default['pagination']['per_page'] = $default['meta']['per_page'];
    $default['pagination']['total'] = $default['meta']['total'];

    unset($default['links']);
    unset($default['meta']);
}
Enter fullscreen mode Exit fullscreen mode

The JSON response structure will be

{
    "data": [
        {}, 
        {}
    ],
    "pagination": {
        "current_page": "",
        "last_page": "",
        "per_page": "",
        "total": ""
    }
}
Enter fullscreen mode Exit fullscreen mode

You may create a base class, define the function and extends the class from resource collection class to prevent code duplication.

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class BaseResourceCollection extends ResourceCollection
{
    public function paginationInformation($request, $paginated, $default)
    {
        $default['pagination']['current_page'] = $default['meta']['current_page'];
        $default['pagination']['last_page'] = $default['meta']['last_page'];
        $default['pagination']['per_page'] = $default['meta']['per_page'];
        $default['pagination']['total'] = $default['meta']['total'];

        unset($default['links']);
        unset($default['meta']);

        return $default;
    }
}
Enter fullscreen mode Exit fullscreen mode
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;

class UserCollection extends BaseResourceCollection
{
    public function toArray(Request $request): array
    {
        return parent::toArray($request);
    }
}
Enter fullscreen mode Exit fullscreen mode

Happy Tinkering ✌️

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (2)

Collapse
 
snehalkadwe profile image
Snehal Rajeev Moon

Well explained.

Collapse
 
phyothiha profile image
Phyo Thiha

Thanks brother. ✌️

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more