Answer the question
In order to leave comments, you need to log in
How to properly organize an intra-site currency transfer transaction between users in laravel?
The user sends a request to the server to transfer part of the funds from his account to the account of another user.
At the database level, the balance field for each user is: unsignedInteger
The request is validated:
$this->validate($request, [
'receiver_id'=>'required',
'money'=>'required|integer',
]);
DB::transaction(function () use ($request) {
//Снимаем деньги со счета пользователя
User::where('id', Auth::user()->id)->decrement('balance', $request->money);
//Переводим деньги другому пользователю
User::where('id', $request->receiver_id)->increment('balance', $request->money);
//Добавляем в бд запись о транзакции
$transaction = MoneyTransaction::create($request->all() + ['sender_id' => Auth::user()->id, 'type_id' => 2]);
});
Answer the question
In order to leave comments, you need to log in
DB::transaction(function () use ($request) {
$user = User::where('id', Auth::user()->id)->lockForUpdate()->first();
if ($user->balance < $request->money) {
throw new \Exception('Insufficient funds');
}
//Снимаем деньги со счета пользователя
$user->decrement('balance', $request->money);
//Переводим деньги другому пользователю
User::where('id', $request->receiver_id)->increment('balance', $request->money);
//Добавляем в бд запись о транзакции
$transaction = MoneyTransaction::create($request->all() + ['sender_id' => $user->id, 'type_id' => 2]);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question