当前位置:网站首页>laravel - query builder 2
laravel - query builder 2
2022-08-08 16:32:00 【Fire like summer xx】
插入语句
insert
- Accepts an array of column names and values
DB::table('users')->insert([
'email' => '[email protected]',
'votes' => 0
]);
- Pass an array of arrays to insert multiple records at once
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
- 插入新记录,Also use subqueries to determine what data should be inserted
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
Update existing records in the database,不存在则创建
Use the first argument's column and value pair to locate the matching database record. 如果记录存在,It will be updated with the value in the second parameter.如果找不到记录,will insert a new record.
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);
- 在操作期间,Specifies additional columns to update
DB::table('users')->increment('votes', 1, ['name' => 'John']);
删除语句
- delete
从表中删除记录.返回受影响的行数
$deleted = DB::table('users')->delete();
$deleted = DB::table('users')->where('votes', '>', 100)->delete();
- truncate
All records will be deleted from the table and will be auto-incremented ID 重置为零
DB::table('users')->truncate();
边栏推荐
- leetcode 31. 下一个排列(实现next_permutation 函数)
- 科创人·优锘科技COO孙岗:错误问题找不到正确答案,求索万物可视的大美未来
- 使用 FasterTransformer 和 Triton 推理服务器加速大型 Transformer 模型的推理
- redis设计与实现 笔记(一)
- 用于视觉语言导航的自监督三维语义表示学习
- 赶紧进来修内功!!!带你认识C语言中各种进制数和原码反码补码.
- 【poi导出excel之XSSFWorkbook】
- 论文解读(soft-mask GNN)《Soft-mask: Adaptive Substructure Extractions for Graph Neural Networks》
- C#/VB.NET convert PDF to PDF/X-1a:2001
- 通过jenkins交付微服务到kubernetes
猜你喜欢
随机推荐
OpenAI怎么写作「谷歌小发猫写作」
赶紧进来修内功!!!带你认识C语言中各种进制数和原码反码补码.
MVCC,主要是为了做什么?
Take you to play with the "Super Cup" ECS features and experiment on the pit [HUAWEI CLOUD is simple and far]
synchronized加载static关键字前和普通方法前的区别?
sql合并连续时间段内,某字段相同的行。
常见的网络安全术语之一
jupyter notebook hide & show all output
C#/VB.NET 将PDF转为PDF/X-1a:2001
10 Top Open Source Caching Tools for Linux in 2020
Redis哨兵的配置和原理
鹏城杯部分WP
【MATLAB项目实战】基于Morlet小波变换的滚动轴承故障特征提取研究
Es的索引操作(代码中的基本操作)
Kubernetes二进制部署高可用集群
Understanding of redis slice cluster
Beetl使用记录
3dsmax2021软件安装教程
phar反序列化
GPT3中文自动生成小说「谷歌小发猫写作」