Last active
May 21, 2023 14:44
-
-
Save ccdyy/676746f7d5f37a462fd733070b5361b4 to your computer and use it in GitHub Desktop.
laravel数据库单表批量更新
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!function_exists('laravel_batch_update')) { | |
/** | |
* laravel数据库单表批量更新,适用于laravel | |
* @param string $table | |
* @param array $list_data | |
* @param int $chunk_size | |
* @return int | |
*/ | |
function laravel_batch_update(string $table, array $list_data, int $chunk_size = 200) | |
{ | |
if (count($list_data) < 1) { | |
throw new \Exception('更新数量不能小于1'); | |
} | |
if ($chunk_size < 1) { | |
throw new \Exception('分切数量不能小于1'); | |
} | |
$chunk_list = array_chunk($list_data, $chunk_size); | |
$count = 0; | |
foreach ($chunk_list as $list_item) { | |
$first_row = current($list_item); | |
$update_col = array_keys($first_row); | |
// 默认以id为条件更新,如果没有ID则以第一个字段为条件 | |
$reference_col = isset($first_row['id']) ? 'id' : current($update_col); | |
unset($update_col[0]); | |
// 拼接sql语句 | |
$update_sql = 'UPDATE ' . $table . ' SET '; | |
$sets = []; | |
$bindings = []; | |
foreach ($update_col as $u_col) { | |
$set_sql = '`' . $u_col . '` = CASE '; | |
foreach ($list_item as $item) { | |
$set_sql .= 'WHEN `' . $reference_col . '` = ? THEN '; | |
$bindings[] = $item[$reference_col]; | |
if ($item[$u_col] instanceof \Illuminate\Database\Query\Expression) { | |
$set_sql .= $item[$u_col]->getValue() . ' '; | |
} else { | |
$set_sql .= '? '; | |
$bindings[] = $item[$u_col]; | |
} | |
} | |
$set_sql .= 'ELSE `' . $u_col . '` END '; | |
$sets[] = $set_sql; | |
} | |
$update_sql .= implode(', ', $sets); | |
$where_in = collect($list_item)->pluck($reference_col)->values()->all(); | |
$bindings = array_merge($bindings, $where_in); | |
$where_in = rtrim(str_repeat('?,', count($where_in)), ','); | |
$update_sql = rtrim($update_sql, ', ') . ' WHERE `' . $reference_col . '` IN (' . $where_in . ')'; | |
// | |
$count += \DB::update($update_sql, $bindings); | |
} | |
return $count; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment