Skip to content

Instantly share code, notes, and snippets.

@salipro4ever
Forked from ctf0/paginate.php
Created December 8, 2018 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salipro4ever/01b8e97952af24ee0330d05e4f350461 to your computer and use it in GitHub Desktop.
Save salipro4ever/01b8e97952af24ee0330d05e4f350461 to your computer and use it in GitHub Desktop.
Laravel Paginate Collection or Array
<?php
/**
* Gera a paginação dos itens de um array ou collection.
*
* @param array|Collection $items
* @param int $perPage
* @param int $page
*
* @return LengthAwarePaginator
*/
public function paginate($items, $perPage = 15, $page = null)
{
$pageName = 'page';
$page = $page ?: (Paginator::resolveCurrentPage($pageName) ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator(
$items->forPage($page, $perPage)->values(),
$items->count(),
$perPage,
$page,
[
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment