In this short video, we are going to use the paginate method of laravel on collection instances. When we are trying to use the paginate() method on collection, it will show the error like: "Method Illuminate\Database\Eloquent\Collection::paginate does not exist.". To avoid this error & use pagination on collection instances, put the below code in AppServiceProvider.
1. First you have to use some classes:
use Illuminate\Support\Collection;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
Then in the boot method, put the following piece of code....
if (!Collection::hasMacro('paginate')) {
Collection::macro('paginate',
function ($perPage = 15, $page = null, $options = []) {
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
return (new LengthAwarePaginator(
$this-(>)forPage($page, $perPage), $this-(>)count(), $perPage, $page, $options))
-(>)withPath('');
});
}
Just with this short code, you will be able to use paginate method on collectioon instances..
If you like this video, Do not forget to share this video & subscribe to the channel. We will bring you such short & useful videos.
For Complete code, refer to this article.
https://learnwithujjwal.com/how-to-us...