Created
December 26, 2019 17:55
-
-
Save grekodev/7d35df55bddbf18126cfed09d340d6bb to your computer and use it in GitHub Desktop.
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 | |
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