Last active
September 22, 2023 01:49
-
-
Save sam-ngu/b7b5bf027cfacbb88171f432164261e0 to your computer and use it in GitHub Desktop.
Laravel Collection Helper: paginate collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
error in index format