dryeyhe0266
dryeyhe0266
2017-09-25 10:46

如何在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 doutou1922 4年前

    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();
    
    点赞 评论 复制链接分享