Skip to content

Instantly share code, notes, and snippets.

@sam-ngu
Last active September 22, 2023 01:49
Show Gist options
  • Save sam-ngu/b7b5bf027cfacbb88171f432164261e0 to your computer and use it in GitHub Desktop.
Save sam-ngu/b7b5bf027cfacbb88171f432164261e0 to your computer and use it in GitHub Desktop.
Laravel Collection Helper: paginate collection
<?php
namespace App\Helpers\General;
use Illuminate\Container\Container;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
class CollectionHelper
{
public static function paginate(Collection $results, $pageSize)
{
$page = Paginator::resolveCurrentPage('page');
$total = $results->count();
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'
));
}
}
@devgene
Copy link

devgene commented Oct 31, 2021

error in index format

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment