Skip to content

Instantly share code, notes, and snippets.

@terremoth
Created January 3, 2025 11:59
Show Gist options
  • Save terremoth/abca98e377387cc75cca73448bf5a0d8 to your computer and use it in GitHub Desktop.
Save terremoth/abca98e377387cc75cca73448bf5a0d8 to your computer and use it in GitHub Desktop.
Laravel Collection Pagination
<?php
namespace App\Resources;
use Illuminate\Container\Container;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
class CollectionHelper
{
public static function paginate(Collection $results, $total, $pageSize)
{
$page = Paginator::resolveCurrentPage('page');
return self::paginator($results->forPage($page, $pageSize), $total, $pageSize, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page',
]);
}
/**
* Create a new length-aware paginator instance.
*
* @param \Illuminate\Support\Collection $items
* @param int $total
* @param int $perPage
* @param int $currentPage
* @param array $options
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
protected static function paginator($items, $total, $perPage, $currentPage, $options)
{
return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact(
'items', 'total', 'perPage', 'currentPage', 'options'
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment