Laravel 4.1:如何对雄辩的渴望关系进行分页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20913606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4.1: How to paginate eloquent eager relationship?
提问by user991
There was question about old L3 eager loaded paginations, not using eloquent. But i want to use eloquent to get eager loaded relationship with pagination.
有关于旧的 L3 急切加载分页的问题,而不是使用 eloquent。但我想使用 eloquent 来获得与分页的急切加载关系。
Main model: Topicthat has one to many relationship with Posts, So that one Topichas many Posts. I get all data with this function:
主要型号:主题有一个与许多关系的帖子,这样一个主题有很多帖子。我使用此功能获取所有数据:
public function findById($id)
{
return $this->topic->with('posts', 'posts.user', 'posts.user.profile')
->find($id);
}
And later I make loop to display all results, but they are not paginated:
后来我循环显示所有结果,但它们没有分页:
@foreach($topic->posts as $post)
... unpaginated content ...
@endforeach
So, i could make a workaround and select separately all posts that has $id of topic and use ->paginate() instead of ->get() and would got paginated $pots,
因此,我可以制定一个解决方法并分别选择所有具有 $id 主题的帖子并使用 ->pagination() 而不是 ->get() 并且会得到分页的 $pots,
- but is there possibility to use eager loaded relationship poststhat is paginated ? And how can it be done ?
- 但是是否有可能使用分页的急切加载关系帖子?怎么做呢?
回答by Aken Roberts
To clarify something: paginating eager-loaded relationships is somewhat of a misconception. The point of eager loading is to retrieve all relationships in as few queries as you can. If you want to retrieve 10 topics, all of which have 35 posts, you will only need two queries. Sweet!
澄清一些事情:对急切加载的关系进行分页是一种误解。急切加载的要点是在尽可能少的查询中检索所有关系。如果要检索 10 个主题,所有主题都有 35 个帖子,则只需要两个查询。甜的!
That said, paginating an eager-loaded relationship is not going to work. Consider two scenarios when eager loading happens:
也就是说,对急切加载的关系进行分页是行不通的。考虑发生急切加载时的两种情况:
You want to retrieve and list topics, and maybe list the first five posts for each topic. Great! Eager loading is perfect. Now, you wouldn't want to paginate the eager-loaded posts on a page like this, so it doesn't matter.
You load a single topic, and you want to paginate the posts for that topic. Great! Relatively easy to do. However, if you've already eager-loaded allposts belonging to this topic, you've just potentially retrieved a lot of extra resources that you don't need. Therefore eager loading is actually hurtingyou.
您想要检索和列出主题,并且可能列出每个主题的前五个帖子。伟大的!急切加载是完美的。现在,您不希望在这样的页面上对预先加载的帖子进行分页,所以这无关紧要。
您加载了一个主题,并且想要为该主题的帖子分页。伟大的!比较容易做到。但是,如果您已经预先加载了属于该主题的所有帖子,那么您可能只是检索了许多您不需要的额外资源。因此,急切加载实际上是在伤害你。
That said, there are two potential solutions:
也就是说,有两种可能的解决方案:
Option 1: Create a custom accessor that paginates the Eloquent relationship.
选项 1:创建一个自定义访问器来对 Eloquent 关系进行分页。
/**
* Paginated posts accessor. Access via $topic->posts_paginated
*
* @return \Illuminate\Pagination\Paginator
*/
public function getPostsPaginatedAttribute()
{
return $this->posts()->paginate(10);
}
Pros: Paginates very easily; does not interfere with normal posts relationship.
Cons: Eager loading posts will not affect this accessor; running it will create two additional queries on top of the eager loaded query.
优点:非常容易分页;不干扰正常的职位关系。
缺点:急切加载帖子不会影响此访问器;运行它将在预先加载的查询之上创建两个额外的查询。
Option 2: Paginate the posts Collection returned by the eager-loaded relationship.
选项 2:对急切加载关系返回的帖子集合进行分页。
/**
* Paginated posts accessor. Access via $topic->posts_paginated
*
* @return \Illuminate\Pagination\Paginator
*/
public function getPostsPaginatedAttribute()
{
$posts = $this->posts;
// Note that you will need to slice the correct array elements yourself.
// The paginator class will not do that for you.
return \Paginator::make($posts, $posts->count(), 10);
}
Pros: Uses the eager-loaded relationship; creates no additional queries.
Cons: Must retrieve ALL elements regardless of current page (slow); have to build the current-page elements manually.
优点:使用急切加载关系;不创建额外的查询。
缺点:无论当前页面如何,都必须检索所有元素(慢);必须手动构建当前页面元素。
回答by psuhas
You may want to take a look at following:
您可能需要查看以下内容:
$category = 'Laravel';
$posts = Post::with('user', 'category')->whereHas('category', function($query) use ($category) {
$query->where('name', '=', $category);
})->paginate();
By Zenry : Source:http://laravel.io/forum/02-25-2014-eager-loading-with-constrains-load-post-based-on-category-name
作者:Zenry:来源:http://laravel.io/forum/02-25-2014-eager-loading-with-constrains-load-post-based-on-category-name
Hope this helps
希望这可以帮助