doujiang1939 2018-11-12 17:05
浏览 30
已采纳

流明 - 分页链接()方法不起作用

I am trying to create a paginated resultset in Lumen. I am not using a database collection, instead it is an array collection.

I have managed to get the results to display, however I am having a problem getting the pagination links() method to work. Here is what I have:

PHP:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

class AppController extends Controller
{
    public function index(Request $request)
    {
        $items = [
            'item1',
            'item2',
            'item3',
            'item4',
        ];

        $collection = collect($items);

        $currentPage = LengthAwarePaginator::resolveCurrentPage();
        $perPage = 2;
        $offset = ($currentPage * $perPage) - $perPage;

        $currentPageResults = $collection->slice($offset, $perPage)->all();

        $paginatedItems = new LengthAwarePaginator($currentPageResults, count($collection), $perPage);

        $paginatedItems->setPath($request->url());

        return view('index', [
            'results' => $paginatedItems,
        ]);
    }
}

View:

<ul>
    @foreach ($results as $result)
        <li>{{ $result }}</li>
    @endforeach
</ul>

<div>
    {{ $results->links() }}
</div>

The error I am getting is:

call_user_func() expects parameter 1 to be a valid callback, no array or string given

If I remove $results->links() I don't get the error.

When I dd($paginationItems) I do get back a valid LengthAwarePaginator object:

enter image description here

  • 写回答

1条回答 默认 最新

  • duanmengsuo9302 2018-11-12 17:32
    关注

    I found the answer:

    Within bootstrap/app.php there is a line of code commented out by default:

    // $app->withEloquent();

    This needs to be uncommented for the pagination links() method to work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探