当前位置:网站首页>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();
边栏推荐
猜你喜欢
论文解读(soft-mask GNN)《Soft-mask: Adaptive Substructure Extractions for Graph Neural Networks》
Web3构架是怎么样的?
‘xxxx‘ is declared but its value is never read.Vetur(6133)
The realization of the salary slip issuing function of WeChat public account + web background
[Unity Starter Plan] Making RubyAdventure02 - Handling Tile Maps & Collision
ESP8266-Arduino编程实例-ADXL345三轴加速计驱动
ctfshow七夕杯复现
有了这个开源工具后,我五点就下班了!
毕设-基于SSM学生考试系统
【有奖征文 第13期】至简致远,“云”响世界,大胆秀出你的华为云技术主张,高额激励等你拿
随机推荐
First online!Messaging middleware fairy notes, covering the essence of Alibaba's ten years of technology
暴力解决MySQL出现的莫名其妙的问题-重启服务!
国泰君安证券新手开户、有安全保障吗?
Metamask插件中-添加网络和切换网络
在通达信开户安全不呢
用于视觉语言导航的自监督三维语义表示学习
spark集群环境搭建
赶紧进来修内功!!!带你认识C语言中各种进制数和原码反码补码.
元宇宙医疗或将改变医疗格局
【MATLAB项目实战】基于Morlet小波变换的滚动轴承故障特征提取研究
DASCTF部分复现
调研阶段复盘
MVCC,主要是为了做什么?
在 Fedora 上使用 SSH 端口转发
使用 PyGame 的冒泡排序可视化工具
egg(二十):fs读取本地的txt文件
最新30系显卡搭建paddle飞浆环境|含CUDA下载安装
laravel - 查询构建器2
phar反序列化
高数_证明_基本初等函数的导数公式