Eloquent batch update of multiple records (update when present, insert when not present)

Source: Internet
Author: User
Tags bulk insert
Eloquent batch update of multiple records (update when present, insert when not present)

It is not a batch assignment of multiple fields to a record.

Similar BULK INSERT:

DB::table('users')->insert(array(  array('email' => 'aaa@example.com', 'name' => 'zhangsan', 'age'=> 0),  array('email' => 'bbb@example.com', 'name' => 'wangwu', 'age'=> 0),  ...));

There are no 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 functions implemented are:

1. When the query conditions exist, batch update the original data;

例:email='aaa@example.com'时, 'age'修改为 20,email='bbb@example.com'时, 'age'修改为 25,...

2. When the query condition does not exist, BULK insert the data.

例:email='ccc@example.com'时, 'age'修改为 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;    // 更新原数据    } else {        $user->email = $data['email'];        $user->age = $data['age'];        $user->save();    }  }  // 批量插入数据  User::insert($insert_array);}

The above code, when the update data for more than thousand pen, there is a performance problem!

For advice, is there a better way to solve it?

Please give me more guidance.

Reply content:

Eloquent batch update of multiple records (update when present, insert when not present)

It is not a batch assignment of multiple fields to a record.

Similar BULK INSERT:

DB::table('users')->insert(array(  array('email' => 'aaa@example.com', 'name' => 'zhangsan', 'age'=> 0),  array('email' => 'bbb@example.com', 'name' => 'wangwu', 'age'=> 0),  ...));

There are no 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 functions implemented are:

1. When the query conditions exist, batch update the original data;

例:email='aaa@example.com'时, 'age'修改为 20,email='bbb@example.com'时, 'age'修改为 25,...

2. When the query condition does not exist, BULK insert the data.

例:email='ccc@example.com'时, 'age'修改为 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;    // 更新原数据    } else {        $user->email = $data['email'];        $user->age = $data['age'];        $user->save();    }  }  // 批量插入数据  User::insert($insert_array);}

The above code, when the update data for more than thousand pen, there is a performance problem!

For advice, is there a better way to solve it?

Please give me more guidance.

1. Change the query to DB action
2. foreach do not have query operation inside
3. All emails can be assembled with a query statement query, which is the current record by the server.

  • 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.