title: Laravel sorting last record
body: <p>I'm bulding a forum. </p>
  
  <p>In the main page, I want to show the last reply for each forum.</p>
  
  <p>The hierarchy is this:</p>
  
  <p><strong>forums</strong></p>
  
  <p>hasMany('App\Topic')</p>
  
  <p>hasManyThrough('App\Topic', 'App\Repley')</p>
  
  <p><strong>topics</strong></p>
  
  <p>belongsTo('App\Forum')</p>
  
  <p>hasMany('App\Reply')</p>
  
  <p><strong>replies</strong></p>
  
  <p>belongsTo('App\Topic')</p>
  
  <p>I did this:</p>
  
  <pre><code> $forum-&gt;replies()-&gt;orderBy('created_at', 'desc')-&gt;first()
  </code></pre>
  
  <p>And it worked. But I want to sort by topics and replies.
  If I post new topic after the last reply, I want that show as the last reply.</p>
  
  <p><strong>Update</strong>:
  I did this, and it work. But is there any other way?</p>
  
  <pre><code>public function last() {
      $topic = $this-&gt;topics-&gt;sortByDesc('created_at')-&gt;first();
      $post = $this-&gt;replies-&gt;sortByDesc('created_at')-&gt;first();
  
      return ($topic-&gt;created_at &gt; $post-&gt;created_at) ? $topic : $post;
  }
  </code></pre>