In new versions of Laravel, you can create your own paginate. Source: https://laravel.com/docs/9.x/pagination
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
/**
* @param Collection $collection
* @param int $perPage
* @param int $currentPage
* @return LengthAwarePaginator
*/
public static function collectionPaginate(Collection $collection, int $perPage, int $currentPage): LengthAwarePaginator
{
$perPage = max($perPage, 1);
$totalPages = ceil($collection->count() / $perPage);
$currentPage = min($totalPages, max($currentPage, 1));
$skip = ($currentPage - 1) * $perPage;
$take = min($collection->count(), $perPage);
$collection = $collection->skip($skip)
->take($take)
->values();
return new LengthAwarePaginator($collection, $totalPages, $perPage);
}
Example:
collectionPaginate(collect([...]), 12, 1);