Laravel pagination not working with array instead of collection

Harry Geo picture Harry Geo · May 27, 2015 · Viewed 10.2k times · Source

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.

Answer

Sh1d0w picture Sh1d0w · May 27, 2015

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)