關於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();
執行以上語句返回正確分頁數據.