Laravel eloquent orderby 两个同名字段

Laravel eloquent orderby two fields with the same name

我有两个表 "Companies" 和 "Properties" 在 属性 class 中定义关系: return $this->belongsTo(Company::class); 我 运行 此 eloquent 查询以获取公司

的所有属性
$properties=Property::with('company')->orderBy('name')->get();

我想不通的是如何先 orderBy Company.name 然后再 orderBy property.name?如果我这样做: orderBy('company.name') 我会得到一个错误。 任何帮助将不胜感激。

您可以将 join() 用作:

$properties = Property::join('companies', 'properties.company_id', '=', 'companies.id')
                      ->orderBy('companies.name')
                      ->orderBy('properties.name')
                      ->select('properties.*')
                      ->with('company')
                      ->get();