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.
Pratheek rebala
source share