public function UpdateAll(string $tableName, array $info)
{
try {
if (count($info) > 0) {
$firstRow = current($info);
$updateColumn = array_keys($firstRow);
$referenceColumn = isset($firstRow['id']) ? 'id' : current($updateColumn);
unset($updateColumn[0]);
$updateSql = "UPDATE " . $tableName . " SET ";
$sets = [];
$bindings = [];
foreach ($updateColumn as $uColumn) {
$setSql = "`" . $uColumn . "` = CASE ";
foreach ($info as $data) {
$setSql .= "WHEN `" . $referenceColumn . "` = ? THEN ? ";
$bindings[] = $data[$referenceColumn];
$bindings[] = $data[$uColumn];
}
$setSql .= "ELSE `" . $uColumn . "` END ";
$sets[] = $setSql;
}
$updateSql .= implode(', ', $sets);
$whereIn = collect($info)->pluck($referenceColumn)->values()->all();
$bindings = array_merge($bindings, $whereIn);
$whereIn = rtrim(str_repeat('?,', count($whereIn)), ',');
$updateSql = rtrim($updateSql, ", ") . " WHERE `" . $referenceColumn . "` IN (" . $whereIn . ")";
return DB::update($updateSql, $bindings);
}
return 0;
} catch (\Exception $e) {
return $e->getMessage();
}
}
调用
$info = [
"id"=>$query->id, //id必传
"consumption_total" => $consumption_total,
"copy_total"=> $copy_total,
"copy_cost_total"=> $copy_cost_total,
"powder_total"=> $powder_total,
"powder_cost_total"=> $powder_cost_total,
"powder_rate_total"=> $powder_rate_total,
];
$this->UpdateAll('your_table',info);
本文链接:https://www.cution.cn/37.html
https://www.pexels.com/zh-cn/collections/1379530-l47756n/
https://www.pexels.com/zh-cn/collections/1379530-f17napd/
https://www.pexels.com/zh-cn/collections/1379530-h8ytruy/
https://www.pexels.com/zh-cn/collections/1379530-8jblapw/
https://www.pexels.com/zh-cn/collections/1379530-rq09wrw/
https://www.pexels.com/zh-cn/collections/1379530-jjkewjv/
https://www.pexels.com/zh-cn/collections/1379530-bwriptj/
https://www.pexels.com/zh-cn/collections/1379530-tmawq1m/
https://www.pexels.com/zh-cn/collections/1379530-0sy22lm/
https://www.pexels.com/zh-cn/collections/1379530-yooiscz/
https://www.pexels.com/zh-cn/collections/1379530-dewn8cg/
https://www.pexels.com/zh-cn/collections/1379530-cgtat9t/
https://www.pexels.com/zh-cn/collections/1379530-9jdvtks/
https://www.pexels.com/zh-cn/collections/1379530-pevbeef/
https://infogram.com/1379530-1h7v4pdmyydjj4k?live
https://infogram.com/1379530-1h7v4pdmyy3xj4k?live
https://infogram.com/1379530-1h7v4pdmyy3784k?live
https://infogram.com/1379530-1hnp27exrr5py4g?live
https://infogram.com/1379530-1hnq41oxee31p23?live