侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Laravel  /  管理员 发布于 2年前   1055

描述: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' => '失败'));
}



  • 上一条:
    laravel如何使用到阿里云短信服务发送消息功能,介绍扩展:MissMyCat/aliyun-sms
    下一条:
    国内用什么翻墙使用谷歌?上外网神器Ghelper插件详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Laravel项目中使用中间件方式统计用户在线时长功能代码示例(0个评论)
    • 在Laravel中构建业务流程模型(0个评论)
    • 在Laravel项目中的实现无密码认证之:发送邮箱链接授权(0个评论)
    • Laravel 10.4版本发布(0个评论)
    • 在laravel框架中的5个HTTP客户端技巧分享(0个评论)
    • 近期文章
    • 如何优雅处理async await错误推荐:await-to-js库(0个评论)
    • lodash工具库(0个评论)
    • 在Laravel项目中使用中间件方式统计用户在线时长功能代码示例(0个评论)
    • 在Laravel中构建业务流程模型(0个评论)
    • windows系统中安装FFMpeg及在phpstudy环境php7.3 + php-ffmpeg扩展的使用流程步骤(0个评论)
    • 在go语言中对浮点的数组、切片(slice)进行正向排序和反向排序(0个评论)
    • 在go语言中对整数数组、切片(slice)进行排序和反向排序(0个评论)
    • 在go语言中对字符串数组、切片(slice)进行排序和反向排序(0个评论)
    • 最新国内免注册ChatGPT体验站_ChatGPT镜像站访问链接地址2023/3/28持续更新(0个评论)
    • 在Laravel项目中的实现无密码认证之:发送邮箱链接授权(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-01
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客