Lumen - 分页 links() 方法不起作用
Lumen - pagination links() method not working
我正在尝试在 Lumen 中创建分页结果集。我没有使用数据库集合,而是数组集合。
我已经设法显示结果,但是我在使用分页 links() 方法时遇到问题。这是我拥有的:
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,
]);
}
}
查看:
<ul>
@foreach ($results as $result)
<li>{{ $result }}</li>
@endforeach
</ul>
<div>
{{ $results->links() }}
</div>
我得到的错误是:
call_user_func() expects parameter 1 to be a valid callback, no array
or string given
如果我删除 $results->links()
我不会收到错误。
当我 dd($paginationItems)
我确实取回了一个有效的 LengthAwarePaginator
对象时:
我找到了答案:
在bootstrap/app.php
中有一行代码被默认注释掉了:
// $app->withEloquent();
为了使分页 links() 方法起作用,需要取消注释。
我正在尝试在 Lumen 中创建分页结果集。我没有使用数据库集合,而是数组集合。
我已经设法显示结果,但是我在使用分页 links() 方法时遇到问题。这是我拥有的:
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,
]);
}
}
查看:
<ul>
@foreach ($results as $result)
<li>{{ $result }}</li>
@endforeach
</ul>
<div>
{{ $results->links() }}
</div>
我得到的错误是:
call_user_func() expects parameter 1 to be a valid callback, no array or string given
如果我删除 $results->links()
我不会收到错误。
当我 dd($paginationItems)
我确实取回了一个有效的 LengthAwarePaginator
对象时:
我找到了答案:
在bootstrap/app.php
中有一行代码被默认注释掉了:
// $app->withEloquent();
为了使分页 links() 方法起作用,需要取消注释。