我正在使用Laravel Luman为一个新闻应用程序构建一个API。到目前为止,API和它提供结果的方式都没有问题,但为了改善移动体验,我计划为结果添加分页。我试了几种方法,但都想不出怎么做。有没有人能建议我一个更好的方法来解决这个问题?
[
{
id: 2,
name: "News",
count: 400, // 400 posts available
posts: [
{
id: 6,
title: "Quick brown fox",
},
{
id: 9,
title: "umps over lazy dog",
},
]我的要求是为post对象添加一个分页。请找到下面的图片作为参考

发布于 2016-02-11 00:40:29
在这里,您需要为急切加载进行分页。
$category = Category::whereId($id)->firstOrFail();
$posts = $category->posts()->paginate();
// then pass both variables to the view
return View::make('some.view', compact('category', 'posts');
// and loop through the products directly
@foreach ($posts as $post)你也可以这样做
$category= Category::with('posts' => function ($q) {
$q->paginate(10);
}])->whereId($id)->firstOrFail();但是使用第二种方法,您将无法进行渲染链接。
https://stackoverflow.com/questions/35320825
复制相似问题