Created
January 3, 2025 11:59
-
-
Save terremoth/abca98e377387cc75cca73448bf5a0d8 to your computer and use it in GitHub Desktop.
Laravel Collection Pagination
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\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