关于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 篇原创文章 · 获赞 18 · 访问量 763

猜你喜欢

转载自blog.csdn.net/qq_37868757/article/details/103257927