Laravel/雄辩的顺序关系在没有原始SQL的情况下下降?

Laravel/雄辩的顺序关系在没有原始SQL的情况下下降?,laravel,eloquent,Laravel,Eloquent,是否可以按关系排序,以便在不使用原始SQL的情况下分发该关系中的记录 例如: $device = CustomerDevice::with('user')->with('customerDeviceHistory')->find($did); customerDeviceHistory中的条目应该是降序的。可能,例如: function customerDeviceHistory() { return $this->hasOne('App\[DeviceHistory]

是否可以按关系排序,以便在不使用原始SQL的情况下分发该关系中的记录

例如:

$device = CustomerDevice::with('user')->with('customerDeviceHistory')->find($did);
customerDeviceHistory
中的条目应该是降序的。

可能,例如:

function customerDeviceHistory() {
   return $this->hasOne('App\[DeviceHistory]', 'id', 'deviceHistory_id')->orderBy('date', 'desc');
}

将orderBy添加到关系函数中

这将按id按
customerDeviceHistory
id的描述顺序排序

$device = CustomerDevice::with('user')->with(['customerDeviceHistory'=>function($query)
    {
        $query->orderBy('id','desc');
    }
])->find($did);

+++++++++++++++