I'm trying to paginate an array data set and it has proven more challenging than I thought.
I'm using Laravel 5
So I have an abstract interface/repository that all my other models extend to and I created a method inside my abstract repository call paginate. I've included both
use Illuminate\Pagination\Paginator;
and
use Illuminate\Pagination\LengthAwarePaginator;
Here is the method
public function paginate($items,$perPage,$pageStart=1)
{
// Start displaying items from this number;
$offSet = ($pageStart * $perPage) - $perPage;
// Get only the items you need using array_slice
$itemsForCurrentPage = array_slice($items, $offSet, $perPage, true);
return new LengthAwarePaginator($itemsForCurrentPage, count($items), $perPage,Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
}
So as you can imagine this function accepts an array of $items
a $perPage
variable that indicates how many to items to paginate and a $pageStart
that indicates from which page to start.
The pagination works and I can see LengthAwarePaginator
instance when I'm doing a dd()
, all of it's values seem fine.
The problem starts when I'm displaying the results.
When I do {!! $instances->render() !!}
The paginator links display's fine, the page
parameter changes according to links but the data is not changing.
Data is the same in every page. When I'm using Eloquent for example Model::paginate(3)
everything works fine, but when I dd()
this LengthAwarePaginator
it's identical to the LengthAwarePaginator
instance of my custom paginator with the exception that it paginates an array ofcourse and not a collection.
you can easily do it laravel pagination with array. step by step solution of laravel paginate from array. We will create our custom collection object with array and create pagination using laravel eloquent.
Pagination works by selecting a specific chunk of results from the database to display to the user. The total number of results is calculated and then split between pages depending on how many results you want to return on a page.
Use Pagination in Laravel Implementing pagination in laravel is smooth, add the laravel paginate function in the getData() function inside the EmployeeController class. Remove all() and use paginate() and it takes a number as an argument, that number defines the number of results to be shown to the user.
You are not passing the current page, like you should so you also get same array. This will work
public function paginate($items,$perPage)
{
$pageStart = \Request::get('page', 1);
// Start displaying items from this number;
$offSet = ($pageStart * $perPage) - $perPage;
// Get only the items you need using array_slice
$itemsForCurrentPage = array_slice($items, $offSet, $perPage, true);
return new LengthAwarePaginator($itemsForCurrentPage, count($items), $perPage,Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
}
Your function will also work if you pass correct value for $pageStart
- Request::get('page', 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With