当前位置:网站首页>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();
边栏推荐
猜你喜欢
![[Unity entry plan] Use the double blood bar method to control the blood loss speed of the damage area](/img/d3/13bff820963988678f3a3361abc3aa.png)
[Unity entry plan] Use the double blood bar method to control the blood loss speed of the damage area

英特尔两大 FPGA 产品已部署至中国创新中心:性能提高 45%,功耗降低 40%

GHOST tool to access the database

用于视觉语言导航的自监督三维语义表示学习

promise学习笔记

【MATLAB项目实战】基于Morlet小波变换的滚动轴承故障特征提取研究

JVM内存Dump原理与在线分析实战

腾讯云产品可观测最佳实践 (Function)

用完华为云会议解决方案,我直接卸载了之前的会议软件【华为云至简致远】

MySQL 数据库
随机推荐
耐心排序——专门快速解决最长递增子数组
开源项目管理解决方案Leantime
鹏城杯部分WP
jupyter notebook hide & show all output
Taro小程序跨端开发入门实战
4、S32K14X学习笔记:S32 Design Studio 新建和导入工程
【有奖征文 第13期】至简致远,“云”响世界,大胆秀出你的华为云技术主张,高额激励等你拿
The origin and creation of Smobiler's complex controls
干货:从零设计高并发架构
急了,Mysql索引中最不容易记的三个知识点通透了
国内部分手机游戏开始显示用户IP属地
Web3构架是怎么样的?
【入门PCB】立创eda的学习
Jingdong T9 pure hand type 688 pages of god notes, SSM framework integrates Redis to build efficient Internet applications
mmdetection最新版食用教程(一):安装并运行demo及开始训练coco
英特尔两大 FPGA 产品已部署至中国创新中心:性能提高 45%,功耗降低 40%
UTF-8 BOM文件导致配置文件无法读取
ImportError: numpy.core.multiarray failed to import [cv2, matplotlib, PyTorch, pyinstaller ]
暴力解决MySQL出现的莫名其妙的问题-重启服务!
OpenAI怎么写作「谷歌小发猫写作」