Skip to content

Instantly share code, notes, and snippets.

@sentiasa
Last active March 15, 2021 11:26
Show Gist options
  • Save sentiasa/4135d0c73e294fe35788a34abe5d7846 to your computer and use it in GitHub Desktop.
Save sentiasa/4135d0c73e294fe35788a34abe5d7846 to your computer and use it in GitHub Desktop.
Laravel Bulk/Mass Update
<?php
$vat = 0.20;
$transactions = Transaction::get();
foreach ($transactions->chunk(5000) as $chunk) {
$cases = [];
$ids = [];
$params = [];
foreach ($chunk as $transaction) {
$cases[] = "WHEN {$transaction->id} then ?";
$params[] = $transaction->profit * $vat;
$ids[] = $transaction->id;
}
$ids = implode(',', $ids);
$cases = implode(' ', $cases);
if (!empty($ids)) {
\DB::update("UPDATE transactions SET `price` = CASE `id` {$cases} END WHERE `id` in ({$ids})", $params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment