简体   繁体   中英

how to sort query results with nested relation column field in laravel?

$props = Property::with(['rentalUnit','rentalUnit.floor'])->get()->toArray();

I want to sort rentalunit collection with field of floor table field floor_name

Any help would be appreciated

Add this to your Property model

public function rentalUnitOrderByFloorName() 
{
    return $this->rentalUnit()->select('rental_units.*', 'floors.name')->leftJoin('floors', 'floors.rental_unit_id', '=', 'rental_units.id')->orderBy('floors.name');
}

Then

$props = Property::with(['rentalUnitOrderByFloorName','rentalUnitOrderByFloorName.floor'])->get()->toArray();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM