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:
-
Modify the
getRowsmethod to handle pagination:- Fetch the data from the API.
- Extract the necessary pagination information (like total pages, current page, etc.).
- Return the paginated data.
-
Create a custom paginator:
- Use Laravel's
LengthAwarePaginatorto paginate the data.
- Use Laravel's
Here's how you can implement it:
Step 1: Modify the getRows Method
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Arr;
use Illuminate\Pagination\LengthAwarePaginator;
public function getRows($page = 1, $perPage = 15)
{
// API request
$response = Http::withHeaders([
'authorization' => "Bearer {$this->apikey}",
'content-type' => 'application/json',
'Client-Token' => config('services.z_api.client_token'),
])->get("https://api.z-api.io/instances?pageSize={$perPage}&page={$page}");
$data = $response->json();
// Map the data
$items = Arr::map($data['content'], function ($item) {
return Arr::only($item, [
'name', 'id', 'token', 'tenant', 'created', 'due', 'paymentStatus',
'deliveryCallbackUrl', 'receivedCallbackUrl', 'disconnectedCallbackUrl',
'messageStatusCallbackUrl', 'receivedAndDeliveryCallbackUrl',
'presenceChatCallbackUrl', 'connectedCallbackUrl', 'receivedStatusCallbackUrl',
'phoneConnected', 'whatsappConnected',
]);
});
// Create a LengthAwarePaginator instance
$paginator = new LengthAwarePaginator(
$items,
$data['total'], // Total items
$perPage, // Items per page
$page, // Current page
['path' => request()->url(), 'query' => request()->query()]
);
return $paginator;
}
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:
-
API Request and Data Mapping:
- The
getRowsmethod fetches data from the API and maps it to the desired format.
- The
-
Pagination:
- The
LengthAwarePaginatoris used to paginate the data. It takes the items, total count, items per page, and current page as parameters. - The
pathandqueryoptions ensure that pagination links are generated correctly.
- The
-
Filament Resource:
- The
paginateUsingmethod is used to paginate the data in the Filament resource. It calls thegetRowsmethod with the current page and items per page.
- The
This approach ensures that your data is paginated correctly when fetched from an external API using Sushi in a Laravel application.