SQL where in 按照 values 排序 with Laravel

假設需求:從 Project 資料表抓取 id 為 2652, 2468, 2534, 2308, 2019 的五筆專案資料

SQL:select * from Project where id in (2652, 2468, 2534, 2308, 2019)

query 後的 result 會自動按照 id 大小排序變為 2019, 2308, 2468, 2534, 2652,但就是想要丟進去的順序阿阿阿~

SQL(修改後):select * from Project where id in (2652, 2468, 2534, 2308, 2019) order by FIELD(id, 2652, 2468, 2534, 2308, 2019)

用 order by 搭配 FIELD 解決!

Laravel

$hotIDs = [2652, 2468, 2534, 2308, 2019];
Project::whereIn('id', $hotIDs)->orderBy(DB::raw("FIELD(id,".implode(",", $hotIDs).")"))->get();