dryeyhe0266 2017-09-25 10:46
浏览 665
已采纳

如何在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

  • 写回答

1条回答 默认 最新

  • doutou1922 2017-09-25 10:55
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?