laravel 使用 Eloquent 对数据进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15533659/
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
Sorting data with Eloquent
提问by Pretty Good Pancake
I'm new to laravel (switched from CI) and eloquent ORM is still a bit of a mystery at some point!
我是 laravel 的新手(从 CI 切换过来的)并且雄辩的 ORM 在某些时候仍然有点神秘!
Here is my problem :
这是我的问题:
I'd like to sort data from my db using Eloquent.
我想使用 Eloquent 对我的数据库中的数据进行排序。
I have a table postsand a table comments(post has many comments and comment belongs to posts)
我有一个表帖子和一个表评论(帖子有很多评论,评论属于帖子)
Every comment has a timestamp (=> created_at field) and I'd like to order things as follow :
每条评论都有一个时间戳(=> created_at 字段),我想按以下方式排序:
(we're on profil page so $user->id is the id of the user (obviously))
(我们在 profil 页面上,所以 $user->id 是用户的 id(显然))
I want every post from posts where this user made a comment and order all those post by the created_at field of the comment
我想要来自该用户发表评论的帖子中的每个帖子,并通过评论的 created_at 字段对所有帖子进行排序
I really want to use Eloquent fully, or at least Fluent, and I don't know the right way to do so.
我真的很想完全使用 Eloquent,或者至少是 Fluent,但我不知道这样做的正确方法。
I hope I'm being clear, and thank you for your time!
我希望我说的很清楚,谢谢你的时间!
回答by normany
I just ran into the same problem on a project I am developing. Everywhere I looked I saw the usort()
or uasort()
as the suggested way to solve this problem. (Collection::sort()
is just a wrapper for uasort()
) but that did not satisfy me because why would I use PHP to sort when SQL should do it for me with an ORDER BY clause??? I ended up implementing it this way using an getXXXAttribute() method:
我刚刚在我正在开发的项目中遇到了同样的问题。我所看到的每一个地方,我都看到usort()
oruasort()
作为解决这个问题的建议方法。(Collection::sort()
只是 ) 的包装器,uasort()
但这并没有让我满意,因为我为什么要使用 PHP 来排序 SQL 应该使用 ORDER BY 子句为我做的事情???我最终使用 getXXXAttribute() 方法以这种方式实现它:
class Post extends Eloquent {
public function comments()
{
return $this->hasMany('Comment');
}
public function getCommentsAttribute()
{
$comments = $this->comments()->getQuery()->orderBy('created_at', 'desc')->get();
return $comments;
}
...
}
回答by dakine
I had the same problem before but it was a one-to-one relationship. This posthelped me. I ended up using a join. My code was like this:
我以前也遇到过同样的问题,但这是一对一的关系。这篇文章帮助了我。我最终使用了连接。我的代码是这样的:
$data = Posts::join('comments', 'comments.post_id', '=', 'posts.id')
->order_by('comments.created_at')
->get(array('comments.field1 as field1', 'posts.field2 as field2'));
回答by Mr. Sensitive
Something like this could help.
像这样的事情可能会有所帮助。
$content = Posts::with(array('comments' => function($query) {
$query->order_by('created_at', 'asc');
}))
->get();
Or if your problem is more complex:
或者,如果您的问题更复杂:
How to sort by a field of the pivot table of a many-to-many relationship in Eloquent ORM
回答by Jason Houle
If you have the relationship defined in your user class then you can get all posts via the comments. It returns a Collection which provides the sortBy() and sortByDesc() functions. Both of these functions will accept a callback where you can choose the sort method yourself.
如果您在用户类中定义了关系,那么您可以通过评论获取所有帖子。它返回一个提供 sortBy() 和 sortByDesc() 函数的 Collection。这两个函数都将接受一个回调,您可以在其中自己选择排序方法。
$posts = $user->comments->post->sortByDesc(function($post) {
return $post->created_at;
});
回答by Pratheek Rebala
The issue you are facing is because the Relationship that you've defined is just giving you the the Comments that are associated with the posts and an "Order By" on that result will just sort the comments since the comments are what is coming out, so what you could do is that also define that "Comments" belongs to "Post" in the Comments model and then find which Post the Comment is associated with and then use the usort()function to run a manual comparison an example would be (I'm writing the code in Laravel 3 but you could re-write it for any other version):
您面临的问题是因为您定义的关系只是为您提供与帖子相关的评论,并且该结果上的“订购者”只会对评论进行排序,因为评论是即将发布的内容,所以你可以做的是在评论模型中定义“评论”属于“发布”,然后找到评论与哪个发布相关联,然后使用usort()函数运行手动比较,一个例子是(我正在 Laravel 3 中编写代码,但您可以为任何其他版本重新编写代码):
So, Assuming that your Comments table has a foreign key called postIDwhich defines the relationship with the Posts table and the Timestamp in the Posts table is the default created_atand the Users table is connected to the Comments table with the foreign key userid,
因此,假设您的 Comments 表有一个名为postID的外键,它定义了与 Posts 表的关系,并且 Posts 表中的 Timestamp 是默认的created_at并且 Users 表使用外键userid连接到 Comments 表,
$userid = $user->id;
$comments = Comments::where_userid($userid);
function commentsort($first, $second){
if (getCommentPost($first) == getCommentPost($second)) {
return 0;
}
return (getCommentPost($first) < getCommentPost($second)) ? -1 : 1;
}
function getCommentPost($comment){
return Post::where_postid($comment->postid)->first()->created_at;
}
usort($comments, "commentsort");
That should help fix this, In reality this isn't included in Laravel because it's a framework and functions like these which perform specific functions aren't generally included in the framework because there is limited scope of use, that being said you could also include this function in the default DB class so as to use this functionality universally in the project.
这应该有助于解决这个问题,实际上这并不包含在 Laravel 中,因为它是一个框架,并且执行特定功能的这些功能通常不包含在框架中,因为使用范围有限,也就是说你也可以包括此功能在默认 DB 类中,以便在项目中普遍使用此功能。