1 $products = Blog::with('user')->where('user_status', 'normal') 2 ->whereIn('blogId', $blogId) 3 ->orderByRaw("FIELD(blogId, " . implode(", ", $blogId) . ")") 4 ->get();
通過laravel框架的預加載功能(with)查詢Blog表,想要對查出的結果集進行排序,問題在於正常情況下,通過數據庫中的某個字段進行asc或者desc排序,但是我想指定一個排序方法怎么辦,這時就需要用到
->orderByRaw("FIELD(blogId, " . implode(", ", $blogId) . ")")
解釋一下其中的參數
blogId是數據庫中要排序的字段
$blogId = {
[10100326],
[10108965],
[10106639],
[10101123]
}
因為orderByRaw接收的參數是字符串,所以需要通過implode把數組轉化為字符串。
