"Optimistic Locking Laravel" Code Answer's
You're definitely familiar with the best coding language PHP that developers use to develop their projects and they get all their queries like "Optimistic Locking Laravel" answered properly. Developers are finding an appropriate answer about Optimistic Locking Laravel related to the PHP coding language. By visiting this online portal developers get answers concerning PHP codes question like Optimistic Locking Laravel. Enter your desired code related query in the search bar and get every piece of information about PHP code related question on Optimistic Locking Laravel.
Optimistic Locking Laravel
<?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: gist.githubusercontent.com
All those coders who are working on the PHP based application and are stuck on Optimistic Locking Laravel can get a collection of related answers to their query. Programmers need to enter their query on Optimistic Locking Laravel related to PHP code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about Optimistic Locking Laravel for the programmers working on PHP code while coding their module. Coders are also allowed to rectify already present answers of Optimistic Locking Laravel while working on the PHP language code. Developers can add up suggestions if they deem fit any other answer relating to "Optimistic Locking Laravel". Visit this developer's friendly online web community, CodeProZone, and get your queries like Optimistic Locking Laravel resolved professionally and stay updated to the latest PHP updates.