Laravel pagination not working with array instead of collection

前端 未结 1 538
情书的邮戳
情书的邮戳 2020-12-18 08:29

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

相关标签:
1条回答
  • 2020-12-18 09:00

    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)

    0 讨论(0)
提交回复
热议问题