Laravel sortBy 分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26383071/
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 sortBy paginate
提问by dulan
I have a poststable and commentstable, comment belongs to post, and I have the relationship setup in Post and Comment model. I did sort posts by the number of comments of each post like this:
我有一个帖子表和评论表,评论属于帖子,我在帖子和评论模型中设置了关系。我确实按照每个帖子的评论数量对帖子进行了排序,如下所示:
$posts = Post::with('comments')->get()->sortBy(function($post) {
return $post->comments->count();
});
What I wonder is how I can paginate these sorted posts?
我想知道的是如何对这些已排序的帖子进行分页?
$posts = Post::with('comments')->get()->sortBy(function($post) {
return $post->comments->count();
})->paginate(20);
doesn't work and gives me error that says paginate is an undefined method.
不起作用,并给我错误说 paginate 是一个未定义的方法。
采纳答案by Marcin Nabia?ek
I don't know if you can do it using Eloquent but you can use join for this:
我不知道你是否可以使用 Eloquent 来做到这一点,但你可以使用 join 来做到这一点:
$posts = Post::leftJoin('comments','posts.id','=','comments.post_id')->
selectRaw('posts.*, count(comments.post_id) AS `count`')->
groupBy('posts.id')->
orderBy('count','DESC')->
paginate(20);
However it seems that in this case all records are taken from database and displayed only those from paginator, so if you have many records it's waste of resources. It seems you should do manual pagination for this:
然而,在这种情况下,似乎所有记录都是从数据库中获取的,并且只显示来自分页器的记录,所以如果你有很多记录,那就是浪费资源。看来您应该为此进行手动分页:
$posts = Post::leftJoin('comments','posts.id','=','comments.post_id')->
selectRaw('posts.*, count(comments.post_id) AS `count`')->
groupBy('posts.id')->
orderBy('count','DESC')->
skip(0)->take(20)->get();
using skip
and take
but I'm not Eloquent expert and maybe there's a better solution to achieve your goal so you can wait and maybe someone will give a better answer.
使用skip
andtake
但我不是 Eloquent 专家,也许有更好的解决方案来实现您的目标,因此您可以等待,也许有人会给出更好的答案。
回答by cwallenpoole
This sounds obvious, but Eloquent will not return a result set here, but rather it will return a collection.
这听起来很明显,但 Eloquent 在这里不会返回结果集,而是会返回一个集合。
If you dig into the source (Builder::get
calls Builder::getFresh
, which calls Builder::runSelect
, which calls Connection::select
), you'll find that it's intention is to simply return the results, which are then placed into a collection (which has the sortBy method).
如果你深入研究源代码(Builder::get
调用Builder::getFresh
、调用Builder::runSelect
、调用Connection::select
),你会发现它的意图是简单地返回结果,然后将结果放入一个集合中(它有 sortBy 方法)。
/**
* Run a select statement against the database.
*
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @return array
*/
public function select($query, $bindings = array(), $useReadPdo = true)
{
return $this->run($query, $bindings, function($me, $query, $bindings) use ($useReadPdo)
{
if ($me->pretending()) return array();
// For select statements, we'll simply execute the query and return an array
// of the database result set. Each element in the array will be a single
// row from the database table, and will either be an array or objects.
$statement = $this->getPdoForSelect($useReadPdo)->prepare($query);
$statement->execute($me->prepareBindings($bindings));
//** this is a very basic form of fetching, it is limited to the PDO consts.
return $statement->fetchAll($me->getFetchMode());
});
}
If you want to have pagination without loading every item, then you need to use @Marcin's solution (duplicated below):
如果您想在不加载每个项目的情况下进行分页,则需要使用@Marcin 的解决方案(复制如下):
$posts = Post::leftJoin('comments','posts.id','=','comments.post_id')->
selectRaw('posts.*, count(comments.post_id) AS `count`')->
groupBy('posts.id')->
orderBy('count','DESC')->
skip(0)->take(20)->get();
回答by cdarken
Just remove the get()
in the chained calls and see what you get, paginate should replace get() call.
只需删除get()
链接调用中的 ,看看你得到了什么,分页应该替换 get() 调用。