title: Laravel Relationship Order By
body: <p>I have two tables <strong>bookings</strong>,<strong>units</strong>,<strong>categories</strong>,<strong>shifts</strong></p>
  
  <p><strong>bookings</strong></p>
  
  <p><em>bookingId</em>
  <em>unitId</em>
  <em>date</em>
  <em>status</em>
  <em>categoryId</em>,
  <em>shiftId</em></p>
  
  <p>I need ordering conditions like below.</p>
  
  <pre><code>$query = $query-&gt;orderBy('date','ASC');
  $query = $query-&gt;orderBy('status','DESC');
  $query = $query-&gt;orderBy('shiftId','ASC');
  $query = $query-&gt;orderBy('unit.name', 'DESC');
  $query = $query-&gt;orderBy('categoryId','ASC');
  </code></pre>
  
  <p>I have created shifts, categories, units relationship in Booking Model.</p>
  
  <pre><code>public function shift(){
    return $this-&gt;hasOne(Shift::class,'shiftId','shiftId');
  }
  public function unit(){
    return $this-&gt;hasOne(ClientUnit::class,'clientUnitId','unitId');
  }
  public function category(){
    return $this-&gt;hasOne(StaffCategory::class,'categoryId','categoryId');
  }
  </code></pre>
  
  <p>But when I tried, the unit name 4th condition of orderBy is not working properly, all other order conditions are on <strong>Bookings</strong>  table and 4th condition is with the relation table.</p>
  
  <p>How can I do it with only relationship. In this projects I am not used any Joins in this project and I will expect a solution without a Join to units table.</p>
  
  <p>Thank You.</p>