In order to be able to paginate custom collections created in laravel using the collection() method add the following code in the AppServiceProvider.php
if (!\Illuminate\Support\Collection::hasMacro('paginate')) {
\Illuminate\Support\Collection::macro('paginate',
function ($perPage = 15, $page = null, $options = []) {
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
return (new \Illuminate\Pagination\LengthAwarePaginator(
$this->forPage($page, $perPage)->values()->all(), $this->count(), $perPage, $page, $options))
->withPath('');
});
}
Source: https://stackoverflow.com/questions/30420505/how-can-i-paginate-a-merged-collection-in-laravel-5
Leave a comment