本文实例讲述了Laravel框架查询构造器 CURD操作。分享给大家供大家参考,具体如下:
新增
?
|
1
2
3
4
5
6
7
8
|
//插入一条数据
public function insert(){
$rs = DB::table('student')->insert([
'name' => 'Kit',
'age' => 12
]);
dd($rs); //true
}
|
?
|
1
2
3
4
5
6
7
8
|
//插入一条数据并返回自增ID
public function insert(){
$id = DB::table('student')->insertGetId([
'name'=>'Tom',
'age'=>11
]);
dd($id); //1004
}
|
?
|
1
2
3
4
5
6
7
8
|
//插入多条数据
public function insert(){
$rs = DB::table('student')->insert([
['name'=>'Ben','age'=>22],
['name'=>'Jean','age'=>23]
]);
dd($rs);//true
}
|
更新
?
|
1
2
3
4
5
6
7
|
//更新一条数据
public function update(){
$rs = DB::table('student')
->where('id',1003)
->update(['age'=>10]);
dd($rs);//1,返回受影响的行数
}
|
?
|
1
2
3
4
5
6
7
8
9
10
11
|
//自增更新
public function update(){
//所有年龄加1
$rs = DB::table('student')->increment('age');
dd($rs);//5,返回受影响的行数
//ID为1001的年龄加3
$rs = DB::table('student')
->where('id',1001)
->increment('age',3);
dd($rs);//1,返回受影响的行数
}
|
?
|
1
2
3
4
5
6
7
8
9
10
11
|
//自减更新
public function update(){
//所有年龄加1
$rs = DB::table('student')->decrement('age');
dd($rs);//5,返回受影响的行数
//ID为1001的年龄加3
$rs = DB::table('student')
->where('id',1001)
->decrement('age',3);
dd($rs);//1,返回受影响的行数
}
|
?
|
1
2
3
4
5
6
7
|
//1001年龄加3并且性别改为11
public function update(){
$rs = DB::table('student')
->where('id',1001)
->increment('age',3,['sex'=>11]);
dd($rs);//1,返回受影响的行数
}
|
删除
?
|
1
2
3
4
5
6
7
|
//删除ID为1006的数据
public function delete(){
$rs = DB::table('student')
->where('id',1006)
->delete();
dd($rs);//1,返回受影响的行数
}
|
?
|
1
2
3
4
5
6
7
|
//删除ID大于1003的数据
public function delete(){
$rs = DB::table('student')
->where('id','>',1003)
->delete();
dd($rs);//2,返回受影响的行数
}
|
?
|
1
2
|
//清空数据表,不返回任何东西
DB::table('student')->truncate();
|
查询
- get
- first
- pluck
- select
?
|
1
2
|
//查询所有数据
$rs = DB::table('student')->get();
|
?
|
1
2
|
//查询第一条数据
$rs = DB::table('student')->orderBy('id','desc')->first();
|
?
|
1
2
3
4
|
//查询一个name字段
$rs = DB::table('student')->pluck('name');
//查询name字段并以ID为键名
$rs = DB::table('student')->pluck('name','id');
|
?
|
1
2
|
//查询name,age,sex字段
$rs = DB::table('student')->select('name','age','sex')->get();
|
聚合函数
?
|
1
2
3
4
5
|
$rs = DB::table('student')->count();
$rs = DB::table('student')->max('age');
$rs = DB::table('student')->min('age');
$rs = DB::table('student')->avg('age');
$rs = DB::table('student')->sum('age');
|
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 40
-
2025-05-29 69
-
2025-06-04 61
-
2025-06-04 62
-
Windows Server 2025 IIS10.0+PHP(FastCGI)+MySQL环境搭建教程
2025-05-26 98
热门评论

