Laravel Tutorials

Maintaining Data Consistency with Laravel Database Transactions

Published
Maintaining Data Consistency with Laravel Database Transactions image

Database transactions serve as a critical safeguard for maintaining data integrity in complex applications. Laravel provides robust transaction capabilities that ensure related database operations execute atomically, guaranteeing that either all changes succeed or none are applied.

Laravel's DB::transaction method offers the most straightforward approach to transaction handling:

use Illuminate\Support\Facades\DB;
 
DB::transaction(function () {
DB::table('accounts')->where('id', 1)->decrement('balance', 100);
DB::table('accounts')->where('id', 2)->increment('balance', 100);
});

This wrapper automatically manages transaction lifecycle, rolling back all changes if any exception occurs within the closure.

Consider a financial application that processes investment portfolio rebalancing operations. Multiple account adjustments must occur simultaneously to maintain accurate financial records:

<?php
 
namespace App\Services;
 
use App\Models\Account;
use App\Models\Transaction;
use App\Models\Portfolio;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
 
class PortfolioRebalancingService
{
public function rebalancePortfolio(Portfolio $portfolio, array $adjustments): bool
{
return DB::transaction(function () use ($portfolio, $adjustments) {
$totalAdjustment = 0;
 
foreach ($adjustments as $adjustment) {
$account = Account::findOrFail($adjustment['account_id']);
 
if ($adjustment['amount'] < 0 && $account->balance < abs($adjustment['amount'])) {
throw new \Exception("Insufficient funds in account {$account->id}");
}
 
$account->increment('balance', $adjustment['amount']);
$totalAdjustment += $adjustment['amount'];
 
Transaction::create([
'account_id' => $account->id,
'portfolio_id' => $portfolio->id,
'amount' => $adjustment['amount'],
'type' => $adjustment['amount'] > 0 ? 'credit' : 'debit',
'description' => 'Portfolio rebalancing',
'processed_at' => Carbon::now(),
]);
}
 
if (abs($totalAdjustment) > 0.01) {
throw new \Exception('Transaction amounts must balance to zero');
}
 
$portfolio->update([
'last_rebalanced_at' => Carbon::now(),
'status' => 'balanced'
]);
 
return true;
}, 3);
}
 
public function transferFunds(Account $fromAccount, Account $toAccount, float $amount): void
{
DB::beginTransaction();
 
try {
if ($fromAccount->balance < $amount) {
throw new \Exception('Insufficient balance for transfer');
}
 
$fromAccount->decrement('balance', $amount);
$toAccount->increment('balance', $amount);
 
Transaction::create([
'account_id' => $fromAccount->id,
'amount' => -$amount,
'type' => 'transfer_out',
'reference_account_id' => $toAccount->id,
'processed_at' => Carbon::now(),
]);
 
Transaction::create([
'account_id' => $toAccount->id,
'amount' => $amount,
'type' => 'transfer_in',
'reference_account_id' => $fromAccount->id,
'processed_at' => Carbon::now(),
]);
 
DB::commit();
 
} catch (\Exception $e) {
DB::rollBack();
throw $e;
}
}
 
public function batchUpdateHoldings(Portfolio $portfolio, array $holdings): void
{
DB::transaction(function () use ($portfolio, $holdings) {
$portfolio->holdings()->delete();
 
foreach ($holdings as $holding) {
$portfolio->holdings()->create([
'symbol' => $holding['symbol'],
'quantity' => $holding['quantity'],
'average_cost' => $holding['average_cost'],
'current_value' => $holding['current_value'],
]);
}
 
$totalValue = collect($holdings)->sum('current_value');
$portfolio->update(['total_value' => $totalValue]);
});
}
}
 
class SubscriptionManager
{
public function upgradePlan(User $user, Plan $newPlan): void
{
DB::transaction(function () use ($user, $newPlan) {
$currentSubscription = $user->subscription;
 
if ($currentSubscription) {
$remainingDays = $currentSubscription->ends_at->diffInDays(Carbon::now());
$creditAmount = ($currentSubscription->plan->price / 30) * $remainingDays;
 
$user->account->increment('credit_balance', $creditAmount);
$currentSubscription->update(['status' => 'cancelled']);
}
 
$user->subscriptions()->create([
'plan_id' => $newPlan->id,
'starts_at' => Carbon::now(),
'ends_at' => Carbon::now()->addMonth(),
'status' => 'active'
]);
 
$user->account->decrement('credit_balance', $newPlan->price);
 
if ($user->account->credit_balance < 0) {
throw new \Exception('Insufficient account balance');
}
});
}
}

Laravel supports transaction retry mechanisms for handling deadlocks, and provides isolation level control for advanced scenarios. The framework also integrates transaction handling with Eloquent models, allowing you to call User::transaction() directly on model classes for more semantic operations.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article