Skip to content

Instantly share code, notes, and snippets.

@pravodev
Created May 21, 2020 16:50
Show Gist options
  • Save pravodev/00791415e1fa727f19ba28dd4a772278 to your computer and use it in GitHub Desktop.
Save pravodev/00791415e1fa727f19ba28dd4a772278 to your computer and use it in GitHub Desktop.
Laravel Pagination on Collection
<?php
namespace App\Providers;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
}
}
// in controller just use paginate like usually
// example
collect([1,2,3,4,5])->paginate(10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment