侯体宗的博客 laravel去重(distinct()

石可破,丹可磨
  • 首页
  • laravel8仿版
  • beego仿版
  • go_聊天
  • 人生
  • 技术
  • php
  • 架构
  • 数据库
  • 更多
    • 文件下载
    • 匿名群聊
    • 群聊(进来吹会!)
    • 留言
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册
    • 设置栏目
    • 更多设置
    • 分割线
laravel去重(distinct())后分页(paginate())数据总数还是显示未去重前的总数解决方案
管理员 发表于 Laravel 中 2020-11-13 11:31:31 浏览量(222)

原因:

看源码得知,这个问题一直存在,因为分页先会执行一遍: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);

效果图:

1.png

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);

效果图:

2.png


3.自定义分页 LengthAwarePaginator  (这里我就不举例了)



去评论 目前还没人评论,做第一个吃螃蟹的人吧!!!
  • 分类目录
  • 人生 (119)
  • 技术 (46)
  • linux (23)
  • blog从零开始 (9)
  • php (48)
  • 架构 (14)
  • 前端 (22)
  • TP(3/5) (14)
  • 数据库 (29)
  • 微信 (2)
  • Laravel (56)
  • Redis (3)
  • Docker (2)
  • Go (8)
  • 最热文章
  • 国内最新最便捷的使用Google方法,无需翻墙,直接输入 (11711)
  • 国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解 (5608)
  • laravel-admin 添加Excel导入功能 (2545)
  • nginx中500,501,502,503,504,50 (1547)
  • laravel-admin表单提交两级联动功能编写 (1337)
  • freenom域名解析步骤 (1297)
  • 建议,想法,思路,来水! (1289)
  • ES调优,对系统设置,配置文件,索引设置等参数详解并设置 (1182)
  • laravel+Guzzle配合正则实现爬虫功能爬取或批 (1069)
  • laravel中whereJsonContains跟wh (1042)
  • 近期文章
  • PHP程序员2021年最...(0个评论)
  • PHP数组的底层实现原理...(0个评论)
  • php7垃圾回收变量的G...(0个评论)
  • 在Laravel中进行类...(0个评论)
  • mysql数据库中事务的...(0个评论)
  • Linux awk 命令...(0个评论)
  • 论不要在mysql中使用...(0个评论)
  • JWT源码实现逻辑详解...(0个评论)
  • Laravel内核分析-...(0个评论)
  • Laravel内核分析-...(0个评论)
  • Laravel内核分析-...(0个评论)
  • 分享Laravel中的1...(0个评论)
  • centos7环境安装g...(0个评论)
  • go+beego框架开...(0个评论)
  • go+beego框架开发...(0个评论)
  • 近期评论
  • 请教 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论

    你好,我也遇到了安装完右...

  • Test11 在

    laravel查询构造器中whereNotKey,whereKey,firstWhere用法详解中评论

    <script&g...

  • 博主 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论

    @西瓜: 每一步都操作完...

  • 西瓜 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论

    你好 我安装完右上角没有...

  • 博主 在

    laravel-admin 添加Excel导入功能中评论

    这应该算是比较详细了吧,...

  • 唐伯虎 在

    laravel-admin 添加Excel导入功能中评论

    咋导入excel

    ...
  • 博主 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论

    希望能帮到你 老铁

  • 疾风神术 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论

    我知道了,是没有打开开发...

  • 文章归档
  • 2016-10 (34)
  • 2016-11 (21)
  • 2017-06 (5)
  • 2017-07 (11)
  • 2017-08 (6)
  • 2017-09 (7)
  • 2017-10 (11)
  • 2017-11 (4)
  • 2017-12 (3)
  • 2018-01 (9)
  • 2018-02 (2)
  • 2018-03 (2)
  • 2018-04 (1)
  • 2018-05 (3)
  • 2018-06 (1)
  • 2018-10 (1)
  • 2018-11 (1)
  • 2020-03 (5)
  • 2020-04 (85)
  • 2020-05 (42)
  • 2020-06 (35)
  • 2020-07 (22)
  • 2020-08 (11)
  • 2020-09 (14)
  • 2020-10 (7)
  • 2020-11 (8)
  • 2020-12 (6)
  • 2021-01 (6)
  • 2021-02 (6)
  • 2021-03 (2)
    go+beego框架开发博客 222 aaa ab压力测试工具 ajax anguler author baidu Base64和Base64URL bbb beego控制器创建 Beego框架安装 beego注册_登录_查询 blog bt ccc CDH5集群搭建 cdn被刷 centos centos6.5 centos7+go环境+beego框架 Centos7免密登录 centos7搭建svn centos7编译安装Swoole cnpm COM cookie crontab定时任务 crontab,shell css dit docker命令 domain easyui easyui-datebox Eloquent优化技巧 ES调优 es迁移 extJS4.2 Facebook爬虫 go+beego开发博客首页 go+beego自定义公共函数 go语言环境安装 harbor header html https,ssl jdk环境安装 jq js
  • 友情链接
  • 侯体宗的博客
  • 烟雨瑟瑟的博客
  • 友链位
Top
  • 友情链接
  • 侯体宗的博客
  • 三防加固笔记本
  • 澜溪博客
  • 心中hope
  • 徒步认知的博客
  • 陈大剩博客
  • 赵波的博客
  • 佘春晓的博客
  • 自动友链系统

Auther ·HouTiZong© 2009-2020 zongscan.com 版权所有ICP证: 粤ICP备20027696号 PHP交流群 也可以扫右边的二维码

侯体宗的博客