Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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 <a href="http://php.net/manual/en/function.usort.php" rel="nofollow" title="**usort**"><strong>usort()</strong></a> 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):</p> <p>So, Assuming that your Comments table has a foreign key called <strong>postID</strong> which defines the relationship with the Posts table and the Timestamp in the Posts table is the default <strong>created_at</strong> and the Users table is connected to the Comments table with the foreign key <strong>userid</strong>,</p> <pre><code> $userid = $user-&gt;id; $comments = Comments::where_userid($userid); function commentsort($first, $second){ if (getCommentPost($first) == getCommentPost($second)) { return 0; } return (getCommentPost($first) &lt; getCommentPost($second)) ? -1 : 1; } function getCommentPost($comment){ return Post::where_postid($comment-&gt;postid)-&gt;first()-&gt;created_at; } usort($comments, "commentsort"); </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload