Skip to content

Instantly share code, notes, and snippets.

@grekodev
Created December 26, 2019 17:55
Show Gist options
  • Save grekodev/7d35df55bddbf18126cfed09d340d6bb to your computer and use it in GitHub Desktop.
Save grekodev/7d35df55bddbf18126cfed09d340d6bb to your computer and use it in GitHub Desktop.
```php
public static function updateValues(array $values)
{
$table = MyModel::getModel()->getTable();
$cases = [];
$ids = [];
$params = [];
foreach ($values as $id => $value) {
$id = (int) $id;
$cases[] = "WHEN {$id} then ?";
$params[] = $value;
$ids[] = $id;
}
$ids = implode(',', $ids);
$cases = implode(' ', $cases);
$params[] = Carbon::now();
return \DB::update("UPDATE `{$table}` SET `value` = CASE `id` {$cases} END, `updated_at` = ? WHERE `id` in ({$ids})", $params);
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment