【发布时间】:2016-06-23 17:18:19
【问题描述】:
我在我的 ForumController 中使用以下代码:
public function boardIndex($id)
{
$container = ForumBoard::with([
'topics' => function($query)
{
$query->orderBy('created_at', 'desc');
$query->paginate(10);
},
'children' => function($query)
{
$query->orderBy('position', 'asc');
}
])->findOrFail($id);
return view('forum.board.index', compact('container'));
}
它按预期工作,我可以通过将 ?page=2 附加到 url 的末尾来手动分页,但是如果我想使用 {!! $container->topics->render() !!} 在我的视图中呈现分页器,我会收到以下错误:
ErrorException in Macroable.php line 81:
Method render does not exist. (View: C:\xampp\htdocs\see\resources\views\forum\board\index.blade.php)
编辑:
感谢您的帮助! 这是我最终的工作代码(它甚至只使用了 4 个数据库查询而不是 5 个):
public function boardIndex($id)
{
$board = ForumBoard::findOrFail($id);
$topics = $board->topics()->latest()->paginate(11);
$children = $board->children()->oldest('position')->get();
return view('forum.board.index', compact('board', 'topics', 'children'));
}
【问题讨论】:
标签: php laravel pagination laravel-5.2 eager-loading