Laravel pagination not working with array instead of collection

坚强是说给别人听的谎言 提交于 2019-11-28 01:41:37

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/30477915/laravel-pagination-not-working-with-array-instead-of-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!