How do database transactions work in Laravel?
Answer
Clear, interview-ready explanation
A database transaction groups related database operations into one atomic unit. Either every operation succeeds and is committed, or an error causes the changes to be rolled back, preventing partially completed work such as an order without its line items.
Laravel’s DB::transaction callback commits automatically when it completes and rolls back when an exception is thrown. Manual beginTransaction, commit, and rollBack methods are available when necessary. Keep transactions short, avoid network calls inside them, and use appropriate locking for concurrent updates when data consistency requires it.