Обновите базу данных с тем же ключом, используя новые значения

Допустим, у меня есть такой стол,

|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

Каков наилучший подход к этому?

1

Решение

Вы можете сделать так:

$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]);
}
0

Другие решения

Laravel Пакетное обновление Модель

Внесите коррективы в Laravel Eloquent, чтобы не делать 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)
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector