Sort data with Eloquent

I'm new to laravel (switched from CI), and the eloquent ORM at some point still remains a mystery!

Here is my problem:

I want to sort data from my db using Eloquent.

I have a posts table and a comments table (mail has a lot of comments and comments on posts)

Each comment has a timestamp (=> created_at), and I would like to order things as follows:

(we are on the profiles page, so $ user-> id is the user id (obviously))

I want each post with messages where this user made a comment and ordered all these messages in the created_at field of the comment

I really want to use Eloquent fully, or at least Fluent, and I don't know how to do it right.

I hope I will clear up and thank you for your time!

+7
source share
5 answers

I just ran into the same problem in the project that I am developing. usort() I looked, I saw usort() or uasort() as a suggested way to solve this problem. ( Collection::sort() is just a wrapper for uasort() ), but that didn’t satisfy me, because why should I use PHP to sort, when SQL should do this for me with ORDER BY clause ??? I ended up implementing it this way using the getXXXAttribute () method:

 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; } ... } 
+13
source

I had the same problem before, but it was a one-to-one relationship. This post has helped me. I have finished using the connection. 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')); 
+2
source

Something like this might help.

 $content = Posts::with(array('comments' => function($query) { $query->order_by('created_at', 'asc'); })) ->get(); 

Or if your problem is more complicated:

How to sort by many-to-many relationship field in the Eloquent ORM

+1
source

If you have relationships defined in your custom class, you can receive all posts through comments. It returns a collection that provides the functions sortBy () and sortByDesc (). Both of these functions will receive a callback when you can choose a sorting method yourself.

 $posts = $user->comments->post->sortByDesc(function($post) { return $post->created_at; }); 
+1
source

The problem you are facing is that the relationship you just give you comments related to messages, and Order By on this result just sorts the comments, because comments are what you could do, so this is also what determines that “Comments” refers to “Post” in the “Comments” model, and then find which comment to “Post Comment” with and then use usort () to start a manual comparison example (I write the code in Laravel 3, but you could rewrite it for any other ver ii):

So, suppose your comment table has a foreign key named postID that defines the relationship with the Posts table, and the Timestamp table in the columns is created_at by default and the Users table is connected to the comment table with the userid foreign key,

  $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"); 

This should help fix it. In fact, this is not included in Laravel, because a structure and functions like these that perform certain functions are usually not included in the structure, since there is a limited scope, it can also include this function in the default DB class to universally use this functionality in the project.

0
source

All Articles