Laravel 5: Manual Pagination From Array.

Laravel

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:

use Illuminate\Pagination\LengthAwarePaginator;

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

$sers = array(
    'Jack',
    'Matt',
    'Hector',
    'Linux',
    'Windows',
    'Mac'
);

Firstly create an variable holding current page value:

$currentPage = LengthAwarePaginator::resolveCurrentPage();

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.

$col = new Collection($users);

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

$perPage = 5;

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

$currentPageSearchResults = $col->slice(($currentPage - 1) * $perPage, $perPage)->all();

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

$entries = new LengthAwarePaginator($currentPageSearchResults, count($col), $perPage);

Pass the data to view and launch it:

return view('my.view', [
    'entries' => $entries,
]);

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

{!! $entries->appends(Input::except('page'))->render() !!}

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

 

 

 

 

Incoming search terms:

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

5 comments

  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