Laravel框架执行原生SQL语句及使用paginate分页的方法
作者:skyisbluening 时间:2023-11-24 00:27:24
本文实例讲述了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框架的PHP程序设计有所帮助。
来源:https://blog.csdn.net/weixin_38682852/article/details/79651879
标签:Laravel,SQL语句,分页
0
投稿
猜你喜欢
Yii框架实现乐观锁与悲观锁流程详解
2023-11-16 13:38:38
JQuery获取表单值
2009-11-19 13:17:00
PHP PDOStatement::errorInfo讲解
2023-06-04 14:11:53
为你的网页添加背景音乐
2007-02-03 11:39:00
对python字典元素的添加与修改方法详解
2023-10-19 09:36:04
Python实现高斯函数的三维显示方法
2023-07-04 01:47:22
javascript创建数组的最简代码
2013-09-01 21:43:04
详解MySQL数据类型之数字类型正确使用
2010-06-20 15:02:00
PHP getName()函数讲解
2023-06-06 08:28:25
Yahoo! BrowserPlus 介绍
2008-06-01 16:38:00
静态页面实现文章点击数统计的js方法
2008-01-23 19:17:00
一个小技巧mysql命令行分页
2011-01-29 16:33:00
如何用ASP建立Index Server查询对象并为其参数赋值?
2010-06-11 19:59:00
前端开发中一些常用技巧总结
2010-07-18 13:13:00
Javascript 动画初探(原理)
2009-02-06 15:53:00
使用Filter实现信息的二次检索
2007-10-08 19:19:00
asp 使用正则表达式替换word中的标签,转为纯文本
2011-02-28 10:49:00
Python中seaborn库之countplot的数据可视化使用
2023-08-10 20:38:56
十万条Access数据表分页的两个解决方法
2008-05-23 18:24:00
在asp中使用js的encodeURIComponent方法
2012-11-30 20:05:53