Eloquent batch update multiple records (update when there is, insert when there is no)

Source: Internet
Author: User
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 queryDBOperation
2.foreachThere 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.