lundi 26 août 2019

Laravel Relationship Order By

I have two tables bookings,units,categories,shifts

bookings

bookingId unitId date status categoryId, shiftId

I need ordering conditions like below.

$query = $query->orderBy('date','ASC');
$query = $query->orderBy('status','DESC');
$query = $query->orderBy('shiftId','ASC');
$query = $query->orderBy('unit.name', 'DESC');
$query = $query->orderBy('categoryId','ASC');

I have created shifts, categories, units relationship in Booking Model.

public function shift(){
  return $this->hasOne(Shift::class,'shiftId','shiftId');
}
public function unit(){
  return $this->hasOne(ClientUnit::class,'clientUnitId','unitId');
}
public function category(){
  return $this->hasOne(StaffCategory::class,'categoryId','categoryId');
}

But when I tried, tHe unit name 4th condition of order By is not working properly, all other order conditions are on Bookings table and 4th condition is with the relation table.

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.

Thank You.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire