当前位置:网站首页>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
边栏推荐
- [Chongqing Guangdong education] National Open University spring 2019 1042 international economic law reference questions
- Flutter 2.10 update details
- Luo Gu Pardon prisoners of war
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- Jcenter () cannot find Alibaba cloud proxy address
- The MySQL team development specifications used by various factories are too detailed. It is recommended to collect them!
- [Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
- Vulnstack3
- Learn tla+ (XII) -- functions through examples
- node工程中package.json文件作用是什么?里面的^尖括号和~波浪号是什么意思?
猜你喜欢
Blue Bridge Cup Square filling (DFS backtracking)
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
Five ways to query MySQL field comments!
Express routing, express middleware, using express write interface
STM32 series - serial port UART software pin internal pull-up or external resistance pull-up - cause problem search
MySQL regexp: Regular Expression Query
One plus six brushes into Kali nethunter
A simple SSO unified login design
Great God developed the new H5 version of arXiv, saying goodbye to formula typography errors in one step, and mobile phones can also easily read literature
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
随机推荐
Restful Fast Request 2022.2.1发布,支持cURL导入
The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
Wechat applet: Xingxiu UI v1.5 WordPress system information resources blog download applet wechat QQ dual end source code support WordPress secondary classification loading animation optimization
La jeunesse sans rancune de Xi Murong
微信小程序:星宿UI V1.5 wordpress系统资讯资源博客下载小程序微信QQ双端源码支持wordpress二级分类 加载动画优化
The server time zone value ‘� й ��� ʱ 'is unrecognized or representatives more than one time zone【
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!
Are you still writing the TS type code
Exploration and Practice of Stream Batch Integration in JD
One plus six brushes into Kali nethunter
When the industrial Internet era is truly developed and improved, it will witness the birth of giants in every scene
R语言用logistic逻辑回归和AFRIMA、ARIMA时间序列模型预测世界人口
Outlook: always prompt for user password
After reading the average code written by Microsoft God, I realized that I was still too young
What sparks can applet container technology collide with IOT
Win: add general users to the local admins group
Redis' hyperloglog as a powerful tool for active user statistics
Runc hang causes the kubernetes node notready
C basic knowledge review (Part 3 of 4)
Heartless sword English translation of Xi Murong's youth without complaint