当前位置:网站首页>Some query constructors in laravel (2)
Some query constructors in laravel (2)
2022-07-05 01:45:00 【phpstory】
310 piece
One 、upsert Method is used to insert a nonexistent record , And update the existing record with the new value you specify . The first parameter of the method consists of the value to be inserted or updated , The second parameter lists the columns that uniquely identify the records in the associated table . The third and last parameter of this method is a column array , If a matching record already exists in the database , Then these columns should be updated :
DB::table('flights')->upsert([
['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
], ['departure', 'destination'], ['price']);
In the example above ,Laravel Will try to insert two records , If the record exists with departure and destination Column with the same value ,Laravel Will update price The value of the column .
Two 、 Sometimes you may want to update existing records in the database , Or if there is no matching record, create it . under these circumstances , have access to updateOrInsert Method . updateOrInsert Method accepts two arguments : An array of conditions for finding records , And an array of key value pairs containing the record to be updated .
updateOrInsert Method will first try to find a matching database record using the key and value pairs of the first parameter . If records exist , Then use the value in the second parameter to update the record . If no record is found , A new record will be inserted , The new data is a collection of two arrays :
DB::table('users')
->updateOrInsert(
['email' => '[email protected]', 'name' => 'John'],
['votes' => '2']
);
3、 ... and 、 to update JSON A field , You can use -> Grammar access JSON Object . Be careful , This operation can only be supported MySQL 5.7+ and PostgreSQL 9.5+ :
$affected = DB::table('users')
->where('id', 1)
->update(['options->enabled' => true]);
notes : If your field is json, You can update a certain attribute value ,options yes users Fields in the table ,enabled Is a value inside , for example :options The value in it is {
"name": "phpstory", "enabled": true}, You can modify the inside enabled value
边栏推荐
- Package What is the function of JSON file? What do the inside ^ angle brackets and ~ tilde mean?
- Database postragesq PAM authentication
- 无心剑英译席慕容《无怨的青春》
- Pytorch common code snippet collection
- Yyds dry goods inventory kubernetes management business configuration methods? (08)
- Database postragesq peer authentication
- C basic knowledge review (Part 3 of 4)
- The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
- Summary of regularization methods
- 微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
猜你喜欢
PowerShell:在代理服务器后面使用 PowerShell
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
Wechat applet; Gibberish generator
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
Interesting practice of robot programming 15- autoavoidobstacles
Visual explanation of Newton iteration method
[OpenGL learning notes 8] texture
Four pits in reentrantlock!
微信小程序:最新wordpress黑金壁纸微信小程序 二开修复版源码下载支持流量主收益
Lsblk command - check the disk of the system. I don't often use this command, but it's still very easy to use. Onion duck, like, collect, pay attention, wait for your arrival!
随机推荐
Game 280 of leetcode week
微信小程序:星宿UI V1.5 wordpress系统资讯资源博客下载小程序微信QQ双端源码支持wordpress二级分类 加载动画优化
187. Repeated DNA sequence - with unordered_ Map basic content
Blue Bridge Cup Square filling (DFS backtracking)
Phpstrom setting function annotation description
Complex, complicated and numerous: illustration of seven types of code coupling
Win:使用 Shadow Mode 查看远程用户的桌面会话
Mysql database | build master-slave instances of mysql-8.0 or above based on docker
Yyds dry goods inventory kubernetes management business configuration methods? (08)
Wechat applet; Gibberish generator
微信小程序;胡言乱语生成器
PHP wechat official account development
Wechat applet: independent background with distribution function, Yuelao office blind box for making friends
Win:将一般用户添加到 Local Admins 组中
Interpretation of mask RCNN paper
微信小程序:最新wordpress黑金壁纸微信小程序 二开修复版源码下载支持流量主收益
ICSI 311 Parser
PHP Joseph Ring problem
【LeetCode】88. Merge two ordered arrays
增量备份 ?db full