title: Laravel eloquent order by subquery
body: <p>I have a problem with ordering by columns in subquery (lastname, firstname).</p>
  
  <p>I already tried this code as suggested by other posts:</p>
  
  <pre><code>-&gt;with(['customer' =&gt; function ($query) {
              $query-&gt;orderBy("lastname", "asc")
                  -&gt;orderBy("firstname", "asc");
          }])
  </code></pre>
  
  <p>Here my full code, but it doesn't work.</p>
  
  <pre><code>return Membership::forCompany($companyId)
          -&gt;whereIn('state', ['ATTIVA', 'IN ATTESA DI ESITO', 'DA INVIARE'])
          -&gt;where(function ($query) {
              $query-&gt;where('end_date', '&gt;=', Carbon::now()-&gt;toDateString())
                  -&gt;orWhereNull('end_date');
          })
          -&gt;with('federation')
          -&gt;with(['customer' =&gt; function ($query) {
              $query-&gt;orderBy("lastname", "asc")
                  -&gt;orderBy("firstname", "asc");
          }]);
  </code></pre>
  
  <p>Here the relationships:</p>
  
  <p>In customer model I have:</p>
  
  <pre><code>public function memberships() {
     return $this-&gt;hasMany('App\Models\Membership');
  }
  </code></pre>
  
  <p>In Membership model I have:</p>
  
  <pre><code>public function customer() {
      return $this-&gt;belongsTo("App\Models\Customer");
  }
  </code></pre>