当前位置:网站首页>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
边栏推荐
- What is the current situation and Prospect of the software testing industry in 2022?
- Yyds dry goods inventory kubernetes management business configuration methods? (08)
- Jcenter () cannot find Alibaba cloud proxy address
- Win: enable and disable USB drives using group policy
- Nebula Importer 数据导入实践
- ICSI 311 Parser
- Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #
- Incremental backup? db full
- Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
- Expansion operator: the family is so separated
猜你喜欢
What sparks can applet container technology collide with IOT
Comment mettre en place une équipe technique pour détruire l'entreprise?
Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Practice of tdengine in TCL air conditioning energy management platform
One plus six brushes into Kali nethunter
Wechat applet: exclusive applet version of the whole network, independent wechat community contacts
如何搭建一支搞垮公司的技術團隊?
微信小程序;胡言乱语生成器
Async/await you can use it, but do you know how to deal with errors?
随机推荐
微信小程序;胡言乱语生成器
如何搭建一支搞垮公司的技術團隊?
Valentine's Day flirting with girls to force a small way, one can learn
Phpstrom setting function annotation description
Mysql database | build master-slave instances of mysql-8.0 or above based on docker
Database postragesql client authentication
Five ways to query MySQL field comments!
179. Maximum number - sort
Yyds dry goods inventory kubernetes management business configuration methods? (08)
Classification of performance tests (learning summary)
Application and development trend of image recognition technology
Express routing, express middleware, using express write interface
Huawei machine test question: longest continuous subsequence
Win: use PowerShell to check the strength of wireless signal
I use these six code comparison tools
Redis(1)之Redis简介
PHP Joseph Ring problem
JVM's responsibility - load and run bytecode
Async/await you can use it, but do you know how to deal with errors?
微信小程序:最新wordpress黑金壁纸微信小程序 二开修复版源码下载支持流量主收益