Home > Backend Development > PHP Tutorial > Eloquent updates multiple records in batches (update if they exist, insert if they do not exist)

Eloquent updates multiple records in batches (update if they exist, insert if they do not exist)

WBOY
Release: 2016-09-23 11:31:01
Original
1893 people have browsed it

Eloquent updates multiple records in batches (update if they exist, insert if they do not exist)

It is not a batch assignment of multiple fields in one record.

Similar to batch insert:

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

Is there any similar sentence:

<code>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' );
</code>
Copy after login
Copy after login

The functions implemented are:

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

<code>例:
email='aaa@example.com'时, 'age'修改为 20,
email='bbb@example.com'时, 'age'修改为 25,
...
</code>
Copy after login
Copy after login

2. When the query conditions do not exist, insert data in batches.

<code>例:
email='ccc@example.com'时, 'age'修改为 50,
...
</code>
Copy after login
Copy after login

My code is:

<code>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);
}
</code>
Copy after login
Copy after login

The above code will cause performance problems when the number of updated data is more than a thousand!

Please advise, is there a better solution?

Please give me more advice.

Reply content:

Eloquent updates multiple records in batches (update if they exist, insert if they do not exist)

It is not a batch assignment of multiple fields in one record.

Similar to batch insert:

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

Is there any similar sentence:

<code>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' );
</code>
Copy after login
Copy after login

The functions implemented are:

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

<code>例:
email='aaa@example.com'时, 'age'修改为 20,
email='bbb@example.com'时, 'age'修改为 25,
...
</code>
Copy after login
Copy after login

2. When the query conditions do not exist, insert data in batches.

<code>例:
email='ccc@example.com'时, 'age'修改为 50,
...
</code>
Copy after login
Copy after login

My code is:

<code>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);
}
</code>
Copy after login
Copy after login

The above code will cause performance problems when the number of updated data is more than a thousand!

Please advise, is there a better solution?

Please give me more advice.

1. Change the query to a DB operation
2. Do not have a query operation in foreach
3. You can assemble all emails into one query statement, and the server will compare which record currently exists

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template