title: How to sort by a field of the pivot table of a many-to-many relationship in Eloquent ORM
body: <p>I have been using Eloquent ORM for some time now and I know it quite well, but I can't do the following, while <strong>it's very easy to do in Fluent</strong>.</p>
  
  <p>I have users with a many-to-many songs, intermediate table being song_user (like it should be). I'd like to get the top songs of a user, judging by the play count. Of course, play count is stored in the intermediate table.</p>
  
  <p>I can do it in Fluent:</p>
  
  <pre><code>$songs = DB::table('songs')
      -&gt;join('song_user', 'songs.id', '=', 'song_user.song_id')
      -&gt;where('song_user.user_id', '=', $user-&gt;id)
      -&gt;orderBy("song_user.play_count", "desc")
      -&gt;get();
  </code></pre>
  
  <p>Easy. But I want to do it in Eloquent, which of course <strong>doesn't work:</strong></p>
  
  <pre><code>$songs = Song::
      with(array("song_user" =&gt; function($query) use ($user) {
          $query-&gt;where("user_id", "=", $user-&gt;id)-&gt;orderBy("play_count", "desc");
      }))
  </code></pre>