Laravel OrderBy Nested Collection
<p>I'm using a Roles package (similar to entrust). I'm trying to sort my User::all() query on roles.id or roles.name</p>

<p>The following is all working</p>

<pre><code>User::with('roles');
</code></pre>

<p>This returns a Collection, with a Roles relation that also is a collection.. Like this:</p>

<p><a href="https://i.stack.imgur.com/pqPcb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pqPcb.png" alt="enter image description here"></a></p>

<blockquote>
  <p>I'm trying to get all users, but ordered by their role ID. </p>
</blockquote>

<p>I tried the following without success
<strong>maybe because 'roles' returns a collection? And not the first role?</strong></p>

<pre><code>return App\User::with(['roles' =&gt; function($query) {
    $query-&gt;orderBy('roles.id', 'asc');
}])-&gt;get();
</code></pre>

<p>And this</p>

<pre><code>return App\User::with('roles')-&gt;orderBy('roles.id','DESC')-&gt;get();
</code></pre>

<p>None of them are working. I'm stuck! Can someone point me in the right direction please?</p>