Is it possible to order by a relationship, so that the records in that relationship are discending without using raw SQL?
Example:
$device = CustomerDevice::with('user')->with('customerDeviceHistory')->find($did);
Entries in customerDeviceHistory should be descending.
This will order by id in desc order of customerDeviceHistory id.
$device = CustomerDevice::with('user')->with(['customerDeviceHistory'=>function($query)
{
$query->orderBy('id','desc');
}
])->find($did);
maybe, for example:
function customerDeviceHistory() {
return $this->hasOne('App\[DeviceHistory]', 'id', 'deviceHistory_id')->orderBy('date', 'desc');
}
Add orderBy to relationship function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With