Last active
March 15, 2021 11:26
-
-
Save sentiasa/4135d0c73e294fe35788a34abe5d7846 to your computer and use it in GitHub Desktop.
Laravel Bulk/Mass Update
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 | |
$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