title: Laravel - orderBy not working on relationship collection
body: <p>I recently moved a Laravel website to a different server with the same setup (slightly different versions of php, mariadb, etc.) and have run into an interesting problem.</p>
  <p>I'm using the package <a href="https://github.com/Team-Tea-Time/laravel-forum" rel="nofollow noreferrer">Team-Tea-Time/laravel-forum</a> (formerly riari/laravel-forum) to provide message board functionalities. In the forum every thread has a link to get to the newest post, which worked fine before the move. After the move those links lead to the oldest/first post instead.</p>
  <p>The functionality is part of the <a href="https://github.com/Team-Tea-Time/laravel-forum/blob/4.0/src/Models/Thread.php" rel="nofollow noreferrer">Thread model</a>, specifically the function getLastPostAttribute().</p>
  <p>When trying to get to the bottom of this, I realized that the orderBy on the posts relationship is simply not having any effect. No matter what attribute I sort by (e.g. created_at, sequence) and which sort direction I request, it's always the same post order and the same first post being returned.</p>
  <p>Example of a thread with 1811 posts with different timestamps and sequence numbers:</p>
  <p>Ordering by sequence ASC works correctly, the first post being the one with the smalles sequence number. This is however also the default order of that relationship (see Model).</p>
  <pre><code>$t-&gt;posts()-&gt;orderBy('sequence', 'asc')-&gt;first();
  Riari\Forum\Models\Post {#3268
      id: 113544,
      thread_id: 3995,
      author_id: 191,
      sequence: 1,
      created_at: &quot;2018-05-08 08:21:20&quot;,
      updated_at: &quot;2018-05-08 10:51:41&quot;,
      deleted_at: null,
      vote_count: 0,
  }
  </code></pre>
  <p>Now when I change the sort order to desc, it should return the post with the highest sequence number. But instead I get the same result as above.</p>
  <pre><code>$t-&gt;posts()-&gt;orderBy('sequence', 'desc')-&gt;first();
  Riari\Forum\Models\Post {#3297
      id: 113544,
      thread_id: 3995,
      author_id: 191,
      sequence: 1,
      created_at: &quot;2018-05-08 08:21:20&quot;,
      updated_at: &quot;2018-05-08 10:51:41&quot;,
      deleted_at: null,
      vote_count: 0,
  }
  </code></pre>
  <p>This is the post that should be returned when sorting by sequence desc:</p>
  <pre><code>Riari\Forum\Models\Post {#3332
      id: 196449,
      thread_id: 3995,
      author_id: 534,
      sequence: 1814,
      created_at: &quot;2020-12-16 14:55:54&quot;,
      updated_at: &quot;2020-12-16 14:55:54&quot;,
      deleted_at: null,
      vote_count: 0,
  }
  </code></pre>
  <p>Has anybody experienced this before? Everything else works fine after the move, I'm clueless as to what would cause such a specific error.</p>