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

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

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题