当前位置:网站首页>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
边栏推荐
- Win: use shadow mode to view the Desktop Session of a remote user
- [Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
- The server time zone value ‘� й ��� ʱ 'is unrecognized or representatives more than one time zone【
- Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
- Tla+ through examples (XI) -- propositional logic and examples
- 无心剑英译席慕容《无怨的青春》
- Global and Chinese market of portable CNC cutting machines 2022-2028: Research Report on technology, participants, trends, market size and share
- 無心劍英譯席慕容《無怨的青春》
- 19. Delete the penultimate node of the linked list
- How to build a technical team that will bring down the company?
猜你喜欢
Nebula Importer 数据导入实践
R language uses logistic regression and afrima, ARIMA time series models to predict world population
Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #
JS implementation determines whether the point is within the polygon range
Behind the cluster listing, to what extent is the Chinese restaurant chain "rolled"?
如何搭建一支搞垮公司的技術團隊?
Kibana installation and configuration
The MySQL team development specifications used by various factories are too detailed. It is recommended to collect them!
Win: use PowerShell to check the strength of wireless signal
流批一體在京東的探索與實踐
随机推荐
Win: enable and disable USB drives using group policy
Educational Codeforces Round 122 (Rated for Div. 2) ABC
Win: use PowerShell to check the strength of wireless signal
Expansion operator: the family is so separated
Interesting practice of robot programming 15- autoavoidobstacles
Numpy library introductory tutorial: basic knowledge summary
PHP 约瑟夫环问题
After reading the average code written by Microsoft God, I realized that I was still too young
Exploration and practice of integration of streaming and wholesale in jd.com
Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
Win:将一般用户添加到 Local Admins 组中
C basic knowledge review (Part 3 of 4)
力扣剑指offer——二叉树篇
"2022" is a must know web security interview question for job hopping
Nebula importer data import practice
One plus six brushes into Kali nethunter
流批一體在京東的探索與實踐
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!
Database postragesq role membership
增量备份 ?db full