CristalTeam / php-api-wrapper

:rainbow: Work with APIs like with Laravel Eloquent or Doctrine (no longer a dream)
MIT License
117 stars 32 forks source link

Handling API pagination #20

Closed ElijahGM closed 3 years ago

ElijahGM commented 3 years ago

How does it handle pagination, or how can i show pagination from the api endpoint?

TZK- commented 3 years ago

Hello,

To get pagination work there are some prerequisite.

Your API must have implemented pagination mechanisms : a filter field used to select the page you want and optionnaly a field to set the number of results per page.

By default, we start from the principle that API uses page and per_page fields to make pagination available.

Then, if you are using the Laravel provider (or the standalone one which works nearly the same) you can do like you would do with Eloquent:

<?php

// Will return a LengthAwarePaginator or an array depending on your provider.
$results = MyApiWrapperModel::paginate(50);

If the query parameters are not the default one supported by our lib, you can specify your own. You'll need to override some pagination constants. You can see these in Cristal\ApiWrapper\Builder::class.

It will need to create a custom Builder and override the newBuilder() method on your Model to return your custom Builder.

If you are using Symfony, it is a little bit more easier and you'll just have to override pagination constants directly on your repository. You can see these in Cristal\ApiWrapper\Bridges\Symfony::class

ElijahGM commented 3 years ago

Thanks it worked well