Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CharlieEtienne/74935daa02c934bac46967984a12fe8e to your computer and use it in GitHub Desktop.
Save CharlieEtienne/74935daa02c934bac46967984a12fe8e to your computer and use it in GitHub Desktop.
This allows you to call ->paginate() on an Eloquent Collection
<?php
use Illuminate\Database\Eloquent\Collection;
// Register in 'boot()' method in app/Providers/AppServiceProvider.php
// This allows you to call ->paginate() on an Eloquent Collection
Collection::macro('paginate', function ($perPage = 10, $current_page = null, $options = []) {
$current_page = $current_page ?: (Paginator::resolveCurrentPage() ?: 1);
return (new LengthAwarePaginator($this->forPage($current_page, $perPage)->values()->all(), $this->count(), $perPage, $current_page, $options))->withPath('');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment