Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel pagination not working with array instead of collection

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.

like image 488
Harry Geo Avatar asked May 27 '15 09:05

Harry Geo


People also ask

Can I paginate an array in Laravel?

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.

How does pagination work in Laravel?

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.

How to do paginate in Laravel?

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.


1 Answers

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)

like image 84
Sh1d0w Avatar answered Sep 17 '22 12:09

Sh1d0w