← ClaudeAtlas

database-transactionslisted

Keep Laravel writes atomic and consistent with transactions, locks, retries, idempotency, side-effect boundaries, and after-commit dispatch.
soden46/syarif-laravel-ai-skills · ★ 4 · Data & Documents · score 78
Install: claude install-skill soden46/syarif-laravel-ai-skills
# Database Transactions Use database transactions for write operations that must be atomic. This is the canonical transaction skill. It consolidates the former `transactions-and-consistency` topic. Transaction boundaries usually belong inside an Action or Service, not spread across controllers. ## Required Transaction Cases Use `DB::transaction()` when a workflow: - writes multiple related records; - updates counters, balances, inventory, or state machines; - coordinates audit records with domain writes; - creates records and related child rows; - must not partially succeed. ```php final class CreateRecord { public function handle(User $actor, array $data): Record { return DB::transaction(function () use ($actor, $data) { $record = Record::create([ 'owner_id' => $actor->id, 'name' => $data['name'], ]); $record->items()->createMany($data['items'] ?? []); return $record->fresh(['items']); }); } } ``` ## Filesystem Side Effects Database rollbacks do not roll back files. Track stored paths and clean them up when the database write fails. ```php $storedPaths = []; try { DB::transaction(function () use ($request, &$storedPaths) { $record = Record::create([...]); foreach ($request->file('attachments', []) as $file) { $storedPaths[] = $file->store("records/{$record->id}", 'public'); } }); } catch (Throwable $exception) {