-
-
Save iRynoh/b3c9da50a9f1ea07909f81a889a474b3 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