Optimistic Locking Laravel

[Solved] Optimistic Locking Laravel | Php - Code Explorer | yomemimo.com
Question : Optimistic Locking Laravel

Answered by : tough-termite-pdevyxabyqp6

<?php
function transfer($fromAccountId, $toAccountId, $balance)
{ $fromQuery = Account::whereId($fromAccountId); if (! $fromQuery->exists()) { throw new InvalidAccountException(); } $toQuery = Account::whereId($toAccountId); if (! $toQuery->exists()) { throw new InvalidAccountException(); } do { $fromAccount = $fromQuery->first(); if ($fromAccount->balance < $amount) { throw new InsufficientBalanceException(); } $updated = Account::whereId($fromAccountId) ->where('updated_at', '=', $fromAccount->updated_at) ->update(['balance' => $fromAccount->balance - $amount]); } while (! $updated); do { $toAccount = $toQuery->first(); $updated = Account::whereId($toAccountId) ->where('updated_at', '=', $toAccount->updated_at) ->update(['balance' => $toAccount->balance + $amount]); } while (! $updated); $transaction = new Transaction(); $transaction->from_account_id = $fromAccountId; $transaction->to_account_id = $toAccountId; $transaction->amount = $amount; $transaction->save();
}

Source : https://gist.githubusercontent.com/aslrousta/4761130124017f57a4eecef7bae9d11d/raw/c6c90dfb6bb552e81f1fb41c87497c7eac396a22/OptimisticTransaction.php | Last Update : Sat, 05 Jun 21

Answers related to optimistic locking laravel

Code Explorer Popular Question For Php