当前位置:网站首页>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();
边栏推荐
- 9. cuBLAS Development Guide Chinese Version--Configuration of Atomic Mode in cuBLAS
- 使用pymongo保存数据到MongoDB的工具类
- promise学习笔记
- 目前安全靠谱的国内期货开户流程?
- NSSCTF部分复现
- Using PyGame's Bubble Sort Visualizer
- leetcode 155. Min Stack最小栈(中等)
- 腾讯云产品可观测最佳实践 (Function)
- leetcode 31. 下一个排列(实现next_permutation 函数)
- 京东T9纯手打688页神笔记,SSM框架整合Redis搭建高效互联网应用
猜你喜欢
基于华为云弹性云服务器ECS(搭载openEuler的鲲鹏通用计算增强型)完成鲲鹏代码迁移工具实践【华为云至简致远】
华为云分布式缓存服务Redis开通及使用规划教程【华为云至简致远】
ESP8266-Arduino编程实例-ADXL345三轴加速计驱动
VIT:Transformer进军CV的里程碑
Patience sorting - specializing in quickly solving the longest increasing subarray
【MATLAB项目实战】基于Morlet小波变换的滚动轴承故障特征提取研究
redis的详细介绍与操作命令
OpenAI怎么写作「谷歌小发猫写作」
ESP8266-Arduino编程实例-ADS1015(ADC)驱动
抓住时代趋势,网赚新逻辑:平台+个人模式超清晰解读(附产品评测)
随机推荐
ImportError: numpy.core.multiarray failed to import [cv2, matplotlib, PyTorch, pyinstaller ]
Kubernetes二进制部署高可用集群
Groovy XML JSON
OpenAI怎么写作「谷歌小发猫写作」
Take you to play with the "Super Cup" ECS features and experiment on the pit [HUAWEI CLOUD is simple and far]
2022年中国全民健身发展白皮书
leetcode/number of palindromic substrings
【云原生】-MySQL压测神器HammerDB的部署及使用
Teach you how to use uniapp to access chat and IM instant messaging - source code sharing
LED显示屏在会议室如何应用
Spark cluster environment construction
有了这个开源工具后,我五点就下班了!
10.cuBLAS开发指南中文版--cuBLAS中的logger配置
Smobiler的复杂控件的由来与创造
json根据条件存入数据库
使用pymongo,将MongoDB生成的ObjectId类型数据与字符串之间的相互转化
firewall高级配置
论文解读(soft-mask GNN)《Soft-mask: Adaptive Substructure Extractions for Graph Neural Networks》
京东T9纯手打688页神笔记,SSM框架整合Redis搭建高效互联网应用
Spam accounts are a lot of trouble, and device fingerprints are quickly found