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把陣列轉化為字串,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/56975.html
標籤:PHP
上一篇:PHP 如何讀取大檔案
