让我们说我有一张这样的桌子,
|id|area_id|area_values|
|1 |12 |value 1 |
|2 |12 |value 2 |
|3 |12 |value 3 |
|4 |01 |value 4 |
,我只想更新面积为12的那些。我的新值是数组$values = ['newvalue1,'newvalue2',newvalue3']
;
我已经尝试过像这样使用array_map。
$ids = Area::where('area_id', 12)->pluck('id')->toArray();
array_map(function ($id) {
array_map(function($areaValue){
Area::find($id)->update(['area_values' => $areaValue]);
}, $values);
}, $ids)
但是我遇到的问题是它仅使用新数组的第一个值newValue1进行更新
这样做的最佳方法是什么?
答案 0 :(得分:0)
您可以这样做:
$values = ['newvalue1','newvalue2','newvalue3'];
$ids = Area::where('area_id', 12)->pluck('id')->toArray();
foreach($ids as $index=>$id){
Area::where('id',$id)->update(['area_values'=>$values[$index]);
}
答案 1 :(得分:0)
Laravel 批量更新模型
对laravel进行调整,使其不进行SQL注入
<?php
namespace App\Models;
use DB;
use Illuminate\Database\Eloquent\Model;
/**
* Students Table Model
*/
class Students extends Model
{
protected $table = 'students';
//Update Batch
public function updateBatch($multipleData = [])
{
try {
if (empty($multipleData)) {
throw new \Exception("The data cannot be empty");
}
$tableName = DB::getTablePrefix() . $this->getTable(); // Table name
$firstRow = current($multipleData);
$updateColumn = array_keys($firstRow);
// The default is to update with id as a condition, and if there is no id, the first field as a condition
$referenceColumn = isset($firstRow['id']) ? 'id' : current($updateColumn);
unset($updateColumn[0]);
// Splicing SQL statement
$updateSql = "UPDATE " . $tableName . " SET ";
$sets = [];
$bindings = [];
foreach ($updateColumn as $uColumn) {
$setSql = "`" . $uColumn . "` = CASE ";
foreach ($multipleData as $data) {
$setSql .= "WHEN `" . $referenceColumn . "` = ? THEN ? ";
$bindings[] = $data[$referenceColumn];
$bindings[] = $data[$uColumn];
}
$setSql .= "ELSE `" . $uColumn . "` END ";
$sets[] = $setSql;
}
$updateSql .= implode(', ', $sets);
$whereIn = collect($multipleData)->pluck($referenceColumn)->values()->all();
$bindings = array_merge($bindings, $whereIn);
$whereIn = rtrim(str_repeat('?,', count($whereIn)), ',');
$updateSql = rtrim($updateSql, ", ") . " WHERE `" . $referenceColumn . "` IN (" . $whereIn . ")";
// Pass in the preprocessed SQL statement and the corresponding binding data
return DB::update($updateSql, $bindings);
} catch (\Exception $e) {
return false;
}
}
}
示例:
// Array of batch updates
$students = [
['id' => 1, 'name' => 'bill', 'area_values' => 'values1'],
['id' => 2, 'name' => 'smith', 'area_values' => 'values2'],
];
// Update batch
app(Students::class)->updateBatch($students);
生成的SQL语句如下:
UPDATE pre_students
SET NAME = CASE
WHEN id = 1 THEN
'bill'
WHEN id = 2 THEN
'smith'
ELSE
NAME
END,
email = CASE
WHEN id = 1 THEN
'values1'
WHEN id = 2 THEN
'values2'
ELSE
email
END
WHERE
id IN (1, 2)