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

fagnerdireito's avatar

how to paginate sushi using external api

Hi, how i can paginate filament resource, using sushi from api.

0 likes
5 replies
LaryAI's avatar
Level 58

To paginate data from an external API using the Sushi package in Laravel, you need to handle the pagination logic manually since Sushi is designed to work with in-memory databases and doesn't inherently support pagination out of the box.

Here's a step-by-step solution to achieve this:

  1. Modify the getRows method to handle pagination:

    • Fetch the data from the API.
    • Extract the necessary pagination information (like total pages, current page, etc.).
    • Return the paginated data.
  2. Create a custom paginator:

    • Use Laravel's LengthAwarePaginator to paginate the data.

Here's how you can implement it:

Step 1: Modify the getRows Method

Step 2: Use the Paginated Data in Your Filament Resource

In your Filament resource, you can now use the paginated data returned by the getRows method. Here's an example of how you might do this:

use App\Models\YourModel;
use Filament\Resources\Table;
use Filament\Resources\Table\Columns\TextColumn;

public static function table(Table $table): Table
{
    return $table
        ->columns([
            TextColumn::make('name')->label('Name'),
            TextColumn::make('id')->label('ID'),
            // Add other columns as needed
        ])
        ->paginateUsing(function ($query, $perPage) {
            $page = request()->get('page', 1);
            return YourModel::getRows($page, $perPage);
        });
}

Explanation:

  1. API Request and Data Mapping:

    • The getRows method fetches data from the API and maps it to the desired format.
  2. Pagination:

    • The LengthAwarePaginator is used to paginate the data. It takes the items, total count, items per page, and current page as parameters.
    • The path and query options ensure that pagination links are generated correctly.
  3. Filament Resource:

    • The paginateUsing method is used to paginate the data in the Filament resource. It calls the getRows method with the current page and items per page.

This approach ensures that your data is paginated correctly when fetched from an external API using Sushi in a Laravel application.

yash_jain04's avatar

@LaryAI did you have anything which will work on filament 3.2 because in latest version this paginate using does not exist : ->paginateUsing(function ($query, $perPage) { $page = request()->get('page', 1); return YourModel::getRows($page, $perPage); });

1 like
As3ad's avatar

after researching, I found out the best solution is to use a frontend framework like react or vue, cuz they have data table packages like tanstack tables that has something called 'server-side pagination'

Please or to participate in this conversation.