当前位置:网站首页>laravel - 查询构建器2
laravel - 查询构建器2
2022-08-08 16:28:00 【流火如夏xx】
插入语句
insert
- 接受一个列名和值的数组
DB::table('users')->insert([
'email' => '[email protected]',
'votes' => 0
]);
- 传递数组数组一次插入多条记录
DB::table('users')->insert([
['email' => '[email protected]', 'votes' => 0],
['email' => '[email protected]', 'votes' => 0],
]);
insertOrIgnore
- 插入数据库时忽略错误
DB::table('users')->insertOrIgnore([
['id' => 1, 'email' => '[email protected]'],
['id' => 2, 'email' => '[email protected]'],
]);
insertUsing
- 插入新记录,同时使用子查询来确定应该插入的数据
DB::table('pruned_users')->insertUsing([
'id', 'name', 'email', 'email_verified_at'
], DB::table('users')->select(
'id', 'name', 'email', 'email_verified_at'
)->where('updated_at', '<=', now()->subMonth()));
自增 IDs
- insertGetId
$id = DB::table('users')->insertGetId(
['email' => '[email protected]', 'votes' => 0]
);
更新语句
update
$affected = DB::table('users')
->where('id', 1)
->update(['votes' => 1]);
updateOrInsert
更新数据库中的现有记录,不存在则创建
使用第一个参数的列和值对来定位匹配的数据库记录。 如果记录存在,它将使用第二个参数中的值进行更新。如果找不到记录,将插入一条新记录。
DB::table('users')
->updateOrInsert(
['email' => '[email protected]', 'name' => 'John'],
['votes' => '2']
);
自增与自减
- increment
DB::table('users')->increment('votes');
DB::table('users')->increment('votes', 5);
- decrement
DB::table('users')->decrement('votes');
DB::table('users')->decrement('votes', 5);
- 在操作期间,指定要更新的其他列
DB::table('users')->increment('votes', 1, ['name' => 'John']);
删除语句
- delete
从表中删除记录。返回受影响的行数
$deleted = DB::table('users')->delete();
$deleted = DB::table('users')->where('votes', '>', 100)->delete();
- truncate
从表中删除所有记录并将自动递增 ID 重置为零
DB::table('users')->truncate();
边栏推荐
猜你喜欢
随机推荐
Grafana配置LDAP认证
手机注册股票开户的流程?网上开户安全?
垃圾账号不胜其烦,设备指纹快速发现
[Unity Starter Plan] Making RubyAdventure02 - Handling Tile Maps & Collision
浅学软件逆向笔记(2)
急了,Mysql索引中最不容易记的三个知识点通透了
找工作的我看了国聘app
目前安全靠谱的国内期货开户流程?
[uniapp applet] view container cover-view
手把手教你uniapp接入聊天IM即时通讯功能-源码分享
【poi导出excel之XSSFWorkbook】
成员变量和局部变量的区别?
Teach you how to use uniapp to access chat and IM instant messaging - source code sharing
MySQL中常见的内些...啥
Go 语言 Strconv 库常用方法
使用 PyGame 的冒泡排序可视化工具
华为云分布式缓存服务Redis开通及使用规划教程【华为云至简致远】
带你玩转“超大杯”ECS特性及实验踩坑【华为云至简致远】
mmdetection最新版食用教程(一):安装并运行demo及开始训练coco
最稳定的淘宝商品详情接口