侯体宗的博客 laravel5+复杂原生sql编写多条件查询定位经纬度距离排序及paginate分页

石可破,丹可磨
  • 首页
  • laravel8仿版
  • beego仿版
  • go_聊天
  • 人生
  • 技术
  • php
  • 架构
  • 数据库
  • 更多
    • 文件下载
    • 匿名群聊
    • 群聊(进来吹会!)
    • 留言
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册
    • 设置栏目
    • 更多设置
    • 分割线

laravel5+复杂原生sql编写多条件查询定位经纬度距离排序及paginate分页

Laravel  /  管理员 发布于 2020-10-29 16:04:25   225

描述:laravel链式操作虽然方便那是针对普通业务,稍微复杂点的我感觉还是原生sql来的实在

话不多说分享一下一个案例:

多条件查询,定位经纬度计算距离附近排序,分页

$where = request()->input();
if (!isset($where['lng']) || !isset($where['lat'])) {return '未传经纬度';}
try {
$sql = "SELECT
 lara_90_cbb_palmlife.id,
 lara_90_cbb_palmlife.tid,
 lara_90_cbb_palmlife.city,
 lara_90_cbb_palmlife.area,
 lara_90_cbb_palmlife.business,
 lara_90_cbb_palmlife.type_name,
 lara_90_cbb_palmlife.img,
 lara_90_cbb_palmlife.title,
 lara_90_cbb_palmlife.lat,
 lara_90_cbb_palmlife.lng,
 lara_90_cbb_palmlife.youhui,
 round(st_distance_sphere(point(lara_90_cbb_palmlife.lng,lara_90_cbb_palmlife.lat),point({$where['lng']},{$where['lat']}))/1000,2) as space
FROM
 `lara_90_cbb_palmlife`
 LEFT JOIN `lara_92_cbb_palmlife_coupon_relation` ON `lara_90_cbb_palmlife`.`tid` = `lara_92_cbb_palmlife_coupon_relation`.`palmlife_id`
 LEFT JOIN `lara_91_cbb_palmlife_coupon` ON `lara_91_cbb_palmlife_coupon`.`cid` = `lara_92_cbb_palmlife_coupon_relation`.`palmlife_coupon_id`
WHERE ";

 if (isset($where['city'])) {
 $sql .= "city = '{$where['city']}'";
 }
 if (isset($where['area'])) {
 $sql .= " and area = '{$where['area']}'";
 }
 if (isset($where['business'])) {
 $sql .= " and business = '{$where['business']}'";
 }
 if (isset($where['type'])) {
 $sql .= " and type_name = '{$where['type']}'";
 }
 if (isset($where['bank'])) {
 $sql .= " and lara_90_cbb_palmlife.bank = {$where['bank']}";
 }
 if (isset($where['bank'])) {//查优惠表
 $sql .= " and (lara_91_cbb_palmlife_coupon.bank = {$where['bank']} or lara_91_cbb_palmlife_coupon.bank is NULL)";
 }
 if (isset($where['week'])) {//查优惠表
 $sql .= " and (youhui LIKE '%周一至周日%' OR lara_91_cbb_palmlife_coupon.title LIKE '%{$where['week']}%' )";
 }
 if (isset($where['kw'])) {
 $sql .= " and lara_90_cbb_palmlife.title like '%{$where['kw']}%'";
 }

 $sql .= "GROUP BY lara_90_cbb_palmlife.tid";
 if (isset($where['space'])) {//1000千米内距离排序  根据定位跟经纬度计算距离
 $sql .= " HAVING space < {$where['space']} ORDER BY space ASC , lara_90_cbb_palmlife.id" ;
 }else{
 $sql .= " ORDER BY space ASC , lara_90_cbb_palmlife.id";
 }
 //$sql .= " LIMIT 100";
 //$res = DB::select($sql);
 $sql =  '('.$sql.') cc';//定义临时表

 $res = DB::table(DB::raw($sql))->paginate(10);

return response()->json(array('status' => 200, 'msg' => '成功', 'res' => $res));
} catch (Exception $e) {
return response()->json(array('status' => 201, 'msg' => '失败'));
}


昵称:

邮箱:

0条评论
最新最热
  • 分类目录
  • 人生 (119)
  • 技术 (46)
  • linux (23)
  • blog从零开始 (9)
  • php (48)
  • 架构 (14)
  • 前端 (22)
  • TP(3/5) (14)
  • 数据库 (29)
  • 微信 (2)
  • Laravel (56)
  • Redis (3)
  • Docker (2)
  • Go (8)
  • 近期文章
  • PHP程序员2021年最新面试题集-持续更新中...(0个评论)
  • PHP数组的底层实现原理浅析(0个评论)
  • php7垃圾回收变量的GC机制详解(0个评论)
  • 在Laravel中进行类型转换详解(0个评论)
  • mysql数据库中事务的四个特征及四种隔离级别的浅析描述(0个评论)
  • Linux awk 命令及统计nginx日志里访问次数最多的前十个IP(0个评论)
  • 论不要在mysql中使用[utf8]编码,如果要用请用[utf8mb4](0个评论)
  • JWT源码实现逻辑详解(0个评论)
  • Laravel内核分析-设计模式之观察者模式(0个评论)
  • Laravel内核分析-设计模式之装饰模式(0个评论)
  • 近期评论
  • 博主 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论 @请教  小图标没有出现 重复检查步骤5,进去看看右下角是否开启..
  • 请教 在

    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解中评论 你好,我也遇到了安装完右上角没有显示图标,也不能打开相关网页的问题,用的是谷歌浏..
  • Test11 在

    laravel查询构造器中whereNotKey,whereKey,firstWhere用法详解中评论 <script>alert(\test\)</script&g..
  • 博主 在

    国内用什么翻墙使用谷歌?上外网神器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)
Top
  • 友情链接
  • 侯体宗的博客
  • 三防加固笔记本
  • 澜溪博客
  • 心中hope
  • 徒步认知的博客
  • 陈大剩博客
  • 赵波的博客
  • 佘春晓的博客
  • 自动友链系统

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

侯体宗的博客