Yes, you can achieve this by customizing the Paginator class in Laravel. Here's how you can do it:
- Create a new class that extends the
Illuminate\Pagination\Paginatorclass:
<?php
namespace App\Pagination;
use Illuminate\Pagination\Paginator;
class ZeroIndexPaginator extends Paginator
{
/**
* Get the current page for the request.
*
* @param string $pageName
* @param int|null $default
* @return int
*/
public function currentPage($pageName = 'page', $default = null)
{
$page = parent::currentPage($pageName, $default);
return $page - 1;
}
}
- In your controller, use the
ZeroIndexPaginatorclass instead of the defaultPaginatorclass:
use App\Pagination\ZeroIndexPaginator;
...
$items = MyModel::paginate(10, ['*'], 'page', $page);
$paginator = new ZeroIndexPaginator($items, 10, $page);
This will create a paginator that starts from zero instead of one.