Maintaining Data Consistency with Laravel Database Transactions

Last updated on by

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

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift

The latest

View all →
Shift + AI = Fully Automated Laravel Upgrades image

Shift + AI = Fully Automated Laravel Upgrades

Read article
Laracon AU 2026 Announces Full Speaker Lineup, Schedule, and Workshops image

Laracon AU 2026 Announces Full Speaker Lineup, Schedule, and Workshops

Read article
Parsel: Parse PDFs, Office Documents, and Images in PHP image

Parsel: Parse PDFs, Office Documents, and Images in PHP

Read article
Typed Objects for Eloquent with Expressive image

Typed Objects for Eloquent with Expressive

Read article
Malware Blocking and Dependency Policies in Composer 2.10 image

Malware Blocking and Dependency Policies in Composer 2.10

Read article
Aegis for Laravel: Scaffolding and Validation Helpers for Value Objects image

Aegis for Laravel: Scaffolding and Validation Helpers for Value Objects

Read article