Eloquent does not assign values to multiple fields of a record in batches when multiple records are updated (update when the record exists and insert when the record does not exist. Similar to batch insert: {code ...} are there similar statements: {code ...} the following functions are implemented: 1. if the query condition exists, update the original data in batches. {generation... eloquent batch update multiple records (update when there is, insert when there is no)
Does not assign values to multiple fields of a record in batches.
Similar to batch insert:
DB::table('users')->insert(array( array('email' => 'aaa@example.com', 'name' => 'zhangsan', 'age'=> 0), array('email' => 'bbb@example.com', 'name' => 'wangwu', 'age'=> 0), ...));
Are there similar statements:
DB::table('users')->update( array( array('email' => 'aaa@example.com', 'name' => 'zhangsan', 'age'=> 20), array('email' => 'bbb@example.com', 'name' => 'wangwu', 'age'=> 25), array('email' => 'ccc@example.com', 'name' => 'chenliu', 'age'=> 50), ...) , 'email' );
The following functions are implemented:
1. update the original data in batches when the query conditions exist;
For example, when email = 'AAA @ example.com ', when 'age' is changed to 20, when email = 'BBB @ example.com', 'age' is changed to 25 ,...
2. if the query condition does not exist, insert data in batches.
For example, when email = 'CCC @ example.com ', 'age' is changed to 50 ,...
My code is:
Public function updateOrCreate (Request $ request) {$ insert_array = []; $ datas = $ request-> all (); foreach ($ datas as $ key => $ data) {$ user = User: where ('email ', $ data ['email'])-> first (); if (! $ User) {$ insert_array [] = $ data; // update original data} else {$ user-> email = $ data ['email ']; $ user-> age = $ data ['age']; $ user-> save () ;}/// batch insert data User: insert ($ insert_array );}
When the above code is updated to more than pieces of data, performance problems may occur!
Is there a better solution to this problem?
Please give more instructions.
Reply content:
Eloquent batch update multiple records (update when there is, insert when there is no)
Does not assign values to multiple fields of a record in batches.
Similar to batch insert:
DB::table('users')->insert(array( array('email' => 'aaa@example.com', 'name' => 'zhangsan', 'age'=> 0), array('email' => 'bbb@example.com', 'name' => 'wangwu', 'age'=> 0), ...));
Are there similar statements:
DB::table('users')->update( array( array('email' => 'aaa@example.com', 'name' => 'zhangsan', 'age'=> 20), array('email' => 'bbb@example.com', 'name' => 'wangwu', 'age'=> 25), array('email' => 'ccc@example.com', 'name' => 'chenliu', 'age'=> 50), ...) , 'email' );
The following functions are implemented:
1. update the original data in batches when the query conditions exist;
For example, when email = 'AAA @ example.com ', when 'age' is changed to 20, when email = 'BBB @ example.com', 'age' is changed to 25 ,...
2. if the query condition does not exist, insert data in batches.
For example, when email = 'CCC @ example.com ', 'age' is changed to 50 ,...
My code is:
Public function updateOrCreate (Request $ request) {$ insert_array = []; $ datas = $ request-> all (); foreach ($ datas as $ key => $ data) {$ user = User: where ('email ', $ data ['email'])-> first (); if (! $ User) {$ insert_array [] = $ data; // update original data} else {$ user-> email = $ data ['email ']; $ user-> age = $ data ['age']; $ user-> save () ;}/// batch insert data User: insert ($ insert_array );}
When the above code is updated to more than pieces of data, performance problems may occur!
Is there a better solution to this problem?
Please give more instructions.
1. change the queryDB
Operation
2.foreach
There are no query operations in it
3. a query statement can be assembled for all emails. the server compares the current record with the current one.