无为清净楼资源网 Design By www.qnjia.com
本文实例讲述了Laravel框架执行原生SQL语句及使用paginate分页的方法。分享给大家供大家参考,具体如下:
1、运行原生sql
public function getList($data){ //获取前端传过来的参数 $user = $data['userId']; $office = $data['officeId']; $key = $data['oneKeySearch']; //进行模糊搜索和联合查询 $where = 'and 1=1 '; if($key!=null) { $where.= ' and ( a.code like "%' . $key . '%"'; $where.= ' or b.name like "%' . $key . '%"'; $where.= ' or c.name like "%' . $key . '%")'; } //对前端传回的字段进行判断,如果不为空则执行条件查询 if($user!=null){ $user='and a.userId='.$user; } if($office!=null){ $office='and a.officeId='.$office; } //自定义原生sql语句,%s可以传参数到sql语句中,格式如下: $sqlTmp=sprintf('select a.id,a.code,a.attendanceRate,a.statisticTime, b.`realName` as userName,c.`name` as officeName from xxxa1 LEFT JOIN xxx2 b ON a.userId=b.id LEFT JOIN xxx3 c ON a.officeId=c.id where a.deleted_at is null and 1=1 %s %s %s ORDER BY a.code ', $where,$office,$user); //执行SQL语句 $results = DB::select($sqlTmp); //返回结果 return $results; }
2、运行查询构建器
public function getList($data){ //获取前端传过来的参数 $user = $data['userId']; $office = $data['officeId']; $key = $data['oneKeySearch']; /* * 1、表格使用别名:直接是 “表名 as table1" ,(下面是xxx1 as a) * 2、左连接:DB::table('表1') * ->leftJoin('表2', '表1.id', '=', '表2.外键关联') * 3、因为使用了软删除,所以在查询的时候要加上 ->whereNull('a.deleted_at') * 4、使用 DB::raw方法创建一个原生表达式,写进要查询的字段名称 * ->select(DB::raw('a.id,a.code,b.`realName` as userName,c.`name` as officeName')) *5、使用orderBy进行排序 * */ $data=DB::table('biz_attendance_sta as a') ->leftJoin('sys_user as b', 'b.id', '=', 'a.userId') ->leftJoin('sys_office as c', 'c.id', '=', 'a.officeId') ->select(DB::raw('a.id,a.code,a.attendanceRate,a.statisticTime, b.`realName` as userName,c.`name` as officeName')) ->whereNull('a.deleted_at') ->orderBy('a.code', 'desc'); //使用 if(!empty(xxx)){},来判断前端传过来的参数是否为空,不为空则执行条件查询 if(!empty($user)){ $data = $data->where( 'a.userId',$user); } if(!empty($office)){ $data = $data->where( 'a.officeId',$office); } //使用 if(!empty(xxx)){},来判断前端传过来的参数是否为空,不为空则执行模糊搜索和联合查询 if (!empty($key)) { $data = $data->where(function ($query) use ($key) { $query->where('a.code', 'like', "%{$key}%") ->orWhere('b.name', 'like', "%{$key}%") ->orWhere('c.name', 'like', "%{$key}%"); }); } //使用->paginate(10)进行分页 $results=$data ->paginate(10); return $results; }
更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC
- 藤泽麻衣《空みあげて》[WAV]
- 纯音入心系列纯音乐《中国古筝经典名曲》1CD[MP3][1.3GB]
- 纯音入心系列纯音乐《古筝新奏:岁月静好与筝语,细水流年与筝同》1CD[MP3][846.9MB]
- 670《脆弱敏感小女生》[320K/MP3][27.53MB]
- 曾琳.2011-好好爱我【南方】【WAV+CUE】
- 范晓萱.1995-RAIN【福茂】【WAV+CUE】