Laravel 8 根據(jù)子查詢進(jìn)行排序

2021-07-19 11:24 更新

此外,查詢構(gòu)建器的 orderBy 函數(shù)也支持子查詢。我們可以使用此功能根據(jù)最后一班航班到達(dá)目的地的時(shí)間對(duì)所有目的地排序。 同樣,這可以只對(duì)數(shù)據(jù)庫執(zhí)行單個(gè)查詢:

return Destination::orderByDesc(
    Flight::select('arrived_at')
        ->whereColumn('destination_id', 'destinations.id')
        ->orderBy('arrived_at', 'desc')
        ->limit(1)
)->get(); 
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)