laravel去重(distinct())后分页(paginate())数据总数还是显示未去重前的总数解决方案
Laravel  /  管理员 发布于 2年前   3126
原因:
看源码得知,这个问题一直存在,因为分页先会执行一遍:select * 获取总数,所以你分页上去重并不会影响 他获取总数
/**
* Paginate the given query.
*
* @param int $perPage
* @param array $columns
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*
* @throws \InvalidArgumentException
*/
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);
$perPage = $perPage ?: $this->model->getPerPage();
$results = ($total = $this->toBase()->getCountForPagination())
? $this->forPage($page, $perPage)->get($columns)
: $this->model->newCollection();
return $this->paginator($results, $total, $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]);
}
案例demo:
1.distinct去重
$res = DB::table('90_cbb_palmlife')
->select('id','tid','title','address','tel','lat','lng',
DB::raw("round(st_distance_sphere(point(lng,lat),point({$where['lng']},{$where['lat']}))/1000,2) as space"
)
)->distinct()
->join('92_cbb_palmlife_coupon_relation', '90_cbb_palmlife.tid', '=', '92_cbb_palmlife_coupon_relation.palmlife_id')
->whereIn('92_cbb_palmlife_coupon_relation.palmlife_coupon_id',$where['cids'])
->orderBy('space','asc')
->paginate(10);
效果图:
2.groupBy去重
$res = DB::table('90_cbb_palmlife')
->select('id','tid','title','address','tel','lat','lng',
DB::raw("round(st_distance_sphere(point(lng,lat),point({$where['lng']},{$where['lat']}))/1000,2) as space"
)
)
->join('92_cbb_palmlife_coupon_relation', '90_cbb_palmlife.tid', '=', '92_cbb_palmlife_coupon_relation.palmlife_id')
->whereIn('92_cbb_palmlife_coupon_relation.palmlife_coupon_id',$where['cids'])
->groupBy('id')
->orderBy('space','asc')
->paginate(10);
效果图:
3.自定义分页 LengthAwarePaginator (这里我就不举例了)
路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..博主 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 @ mashrdn 多切换几个节点测试,免费ssr是没那么稳..mashrdn 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 V2rayn免费节点添加上去了,youtobe无法打开网页,是怎么回事..张伟 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 3q!有用,不过免费节点隔天就要去git上复制新的导进去..博主 在
科学上网翻墙访问Google , 上外网神器佛跳墙VPN(永久免费)使用流程步骤中评论 该篇教程已不能用了,告知大家,免的老有老铁问我!..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号