方法paginate不存在Laravel 5.2

时间:2016-03-20 08:09:04

标签: php laravel pagination

当我尝试在Laravel 5.2中进行分页时,出现以下错误。

  

Macroable.php第81行中的BadMethodCallException:方法paginate   不存在。

代码:

Illuminate\Contracts\Pagination\Paginator;
...
       $count = 0;
        $gdprecords = Gdprecord::all();
        foreach($gdprecords as $gdprecord) {
            $count++;
            $Pa = $gdprecord->adultpopulation;
            $Pc = $gdprecord->childpopulation;
            $P = $Pa + $Pc;  // total population
            $T = ($gdprecord->gdpcapita * $P) * $gdpratio;  // basic income for country 
            if($bi_ratio == 0) { 
                $gdprecord->bi_adult = round($T / $Pa, 2);
            } else {
                $gdprecord->bi_adult = round($T / ($Pa + ($Pc / $bi_ratio)), 2);
            }
            $gdprecord->bi_adult_monthly = round($gdprecord->bi_adult / 12, 2);
            $gdprecord->bi_adult_daily = round($gdprecord->bi_adult / 365, 2);
            if($bi_ratio == 0) {
                $gdprecord->bi_child = 0;
            } else {
                $gdprecord->bi_child = round($gdprecord->bi_adult / $bi_ratio, 2);
            }
            $gdprecord->bi_child_monthly = round($gdprecord->bi_child / 12, 2);
            $gdprecord->bi_child_daily = round($gdprecord->bi_child / 365, 2);                    
        }
        $sorted_gdprecords = $gdprecords->sortByDesc('bi_adult')->paginate(25);

在数组排序之前,我不想分页。我如何使这项工作?

谢谢,菲利普

1 个答案:

答案 0 :(得分:0)

这是我找到的解决方案,借用其他人的代码:

    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()));
    }

并添加此代码:

  $gdprecords2 = $gdprecords->sortByDesc('bi_adult')->all();
  $perPage = 25;
  $sorted_gdprecords = self::paginate($gdprecords2, $perPage);

感谢您的帮助。 -Philip