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 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制