当前位置:网站首页>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
边栏推荐
- Main window in QT application
- Practice of tdengine in TCL air conditioning energy management platform
- Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide
- Wechat applet: the latest WordPress black gold wallpaper wechat applet two open repair version source code download support traffic main revenue
- Es uses collapsebuilder to de duplicate and return only a certain field
- The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
- Win: use PowerShell to check the strength of wireless signal
- Can financial products be redeemed in advance?
- Blue Bridge Cup Square filling (DFS backtracking)
- MySQL backup and recovery + experiment
猜你喜欢

MATLB|多微电网及分布式能源交易

MySQL REGEXP:正则表达式查询

流批一體在京東的探索與實踐

Kibana installation and configuration

Nebula Importer 数据导入实践
![[swagger]-swagger learning](/img/60/1dbe074b3c66687867192b0817b553.jpg)
[swagger]-swagger learning

线上故障突突突?如何紧急诊断、排查与恢复

The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by

Interesting practice of robot programming 15- autoavoidobstacles

流批一体在京东的探索与实践
随机推荐
Exploration and practice of integration of streaming and wholesale in jd.com
Roads and routes -- dfs+topsort+dijkstra+ mapping
Global and Chinese market of portable CNC cutting machines 2022-2028: Research Report on technology, participants, trends, market size and share
Win: add general users to the local admins group
Huawei machine test question: longest continuous subsequence
JS implementation determines whether the point is within the polygon range
[OpenGL learning notes 8] texture
增量备份 ?db full
Database postragesq BSD authentication
Package What is the function of JSON file? What do the inside ^ angle brackets and ~ tilde mean?
MySQL backup and recovery + experiment
MATLB | multi micro grid and distributed energy trading
PHP wechat official account development
Educational Codeforces Round 122 (Rated for Div. 2) ABC
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
MySQL regexp: Regular Expression Query
Wechat applet: independent background with distribution function, Yuelao office blind box for making friends
C basic knowledge review (Part 3 of 4)
Wechat applet; Gibberish generator
Numpy library introductory tutorial: basic knowledge summary