Outsource Laravel Development Partner - $3200/Month | Bacancy

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
Jump24 - UK Laravel Agency

Laravel Developers that Click into Place. Never outsourced. Never offshored. Always exceptional.

Visit Jump24 - UK Laravel Agency
Tinkerwell logo

Tinkerwell

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

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

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

PhpStorm
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
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
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

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

Shift
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
Lucky Media logo

Lucky Media

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

Lucky Media
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

The latest

View all →
A Clean API for Reading PHP Attributes image

A Clean API for Reading PHP Attributes

Read article
Serve Markdown Versions of Your Laravel Pages to AI Agents image

Serve Markdown Versions of Your Laravel Pages to AI Agents

Read article
The Inertia v3 Beta is Here image

The Inertia v3 Beta is Here

Read article
Polyscope Is an Ai-First Dev Environment for Orchestrating Agents image

Polyscope Is an Ai-First Dev Environment for Orchestrating Agents

Read article
Filament v5.3.0 Released with Deferred Tab Badges and Column Manager Improvements image

Filament v5.3.0 Released with Deferred Tab Badges and Column Manager Improvements

Read article
Ward: A Security Scanner for Laravel image

Ward: A Security Scanner for Laravel

Read article