Laravel 5: Manual Pagination From Array.


Laravel documents is not covering anything about making a pagination from your custom array. its very nice and easy to use pagination out of the box with eloquent queries, but if you want to know how to do it with your custom arrays then you might find it hard searching around in their website.

Anyway here is the easy and workable way of making a pagination out of your custom data array.

As the Laravel docs says you can either use Illuminate\Pagination\LengthAwarePaginator or use Illuminate\Pagination\Paginator which in this tutorial I’m going to use LengthAwarePaginator.

So I’m going to call it in via:

Lets assume we want to paginate an array called $users.

Firstly create an variable holding current page value:

Now Lets convert our array into a Laravel Collection. This will allow us to use slice and other methods on the data later on. Don’t forget to bring in the collection class.

Create a variable holding number of items you wish to show on each page:

Slice your array based on the items you wish to show on each page and the current page number:

Now lets save the Pagination in a variable which we are going to pass to our view:

Pass the data to view and launch it:

Now you can show your values in your view with a loop. after that you can show the pagination in the view:

Well that is it. Hope it helps some of you out there.





Incoming search terms:

  • manual pagination laravel
  • laravel pagination manually
  • LengthAwarePaginator tutorial
  • laravel create paginator manually
  • laravel 5 5 array LengthAwarePaginator
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1833 X3d30q1zapfSF5anEVG8CHJJtiYuqauEXWu6cBUUMlxXh9bgsY4lsWFZNm5RM73Q c91255432ca39d5229272aa43ee9199f8412f5ee&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1827 UAkOzv3wrd9fBEiYcvGhpXPUTOAjmHCuuUNNVxTXrC9DRjbRCeFCKO-Ej_4i3r57 47163ab6ecf4a2bf24f0789c1d84eafbb8da4694&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • find in array laravel
  • create manual pagination
  • print paginate current page laravel

5 thoughts on “Laravel 5: Manual Pagination From Array.”

  1. I have applied this code. It shows 1st page correctly but when i clicked any page number, it redirects to my home page. Please issue a solution.

  2. Correct way:
    $entries = new LengthAwarePaginator($currentPageSearchResults, count($col), $perPage, $currentPage,[‘path’ => LengthAwarePaginator::resolveCurrentPath()] );

    (two more arguments in LenghtAwarePaginator: Current Page and Current Path as an array option).
    Worked for me.

Leave a Reply