關於laravel中paginate()與distinct()衝突及解決

關於laravel中paginate()與distinct()衝突及解決

$result = DB::table('students')
                ->where('is_china', true)
                ->orderByDesc('created_at')
                ->distinct()
                ->paginate();

執行以上語句可以得到: 去重後的結果是對的,但是分頁後顯示的 total 還是沒去重之前的總數;

解決方法:

使用 groupBy() 實現去重的效果

$result = DB::table('students')
                ->where('is_china', true)
                ->groupBy('id')
                ->orderByDesc('created_at')
                ->paginate();

執行以上語句返回正確分頁數據.

發佈了14 篇原創文章 · 獲贊 20 · 訪問量 3350
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章