L
laravel:transactions-and-consistency
About laravel:transactions-and-consistency
A Laravel skill for making multi-step database changes atomic and handling retries safely. It covers transaction boundaries, after-commit work, idempotent jobs, and row locking.
Key features
- 01Wrap related writes in transactions
- 02Defer events and jobs until commit
- 03Design idempotent jobs
- 04Coordinate writes with row locks
- 05Validate invariants before transactions
Use cases
- 01Update records and related items atomically
- 02Queue jobs only after a successful commit
- 03Prevent duplicate effects when jobs retry
Install
Copy the command and run it in a terminal.
$
npx skills add https:/ / github.com/ jpcaparas/ superpowers-laravel --skill laravel:transactions-and-consistencyAfter installing, type/laravel:transactions-and-consistencyin Claude Code to confirm it loaded.
Details
From the project READMETransactions and Consistency
Ensure multi-step changes are atomic; make retries safe.
Commands
DB::transaction(function () use ($order, $payload) {
$order->update([...]);
$order->items()->createMany($payload['items']);
OrderUpdated::dispatch($order); // or flag for after-commit
});
// Listener queued after commit
class SendInvoice implements ShouldQueue {
public $afterCommit = true;
}
Patterns
- Use
DB::transactionto wrap write sequences and related side-effects - Prefer
$afterCommitordispatchAfterCommit()for events / jobs - Make jobs idempotent (check existing state, use unique constraints)
- Use
lockForUpdate()for row-level coordination when needed - Validate invariants at the boundary before starting the transaction
- Laravel 13+:
upsert()throwsInvalidArgumentExceptionon an emptyuniqueBy— always pass the real unique columns
Comments
Markdown supported