Skip to content

Instantly share code, notes, and snippets.

@ericvanjohnson
Last active April 21, 2021 02:09
Show Gist options
  • Save ericvanjohnson/defd5302a2bfb80d1a3499ea2c7a599e to your computer and use it in GitHub Desktop.
Save ericvanjohnson/defd5302a2bfb80d1a3499ea2c7a599e to your computer and use it in GitHub Desktop.
Laravel Paginate Collection or Array
<?php
use Illuminate\Support\Collection;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* Paginator for a Collection
*
* @param Collection $items
* @param int $perPage
* @param int $page
* @param array $options
*
* @return LengthAwarePaginator
*/
public function paginate($items, $perPage = 15, $page = null, $options = [])
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment