2016-10-03 3 views
2

関係で注文することができるので、その関係のレコードは生のSQLを使わずに紛失することはありますか?Laravel/Eloquent order関係Raw SQLなしの降順?

例:customerDeviceHistory

$device = CustomerDevice::with('user')->with('customerDeviceHistory')->find($did); 

エントリが下降されるべきです。

答えて

2

をORDERBYを追加します。

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

多分、例えば:

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

これはcustomerDeviceHistory IDの降順順にidで注文します関係関数に

+1

非常に良い、ありがとう。 – Scarwolf

+0

++++++++++++++++ –