15/04/2026
Laravel Tips & Tricks
Sometimes we need to dispatch events after database changes, as shown in this picture.
The Problem:
If something goes wrong and the transaction rolls back, the event has already been dispatched.
There are several solutions for this issue:
One:
add public $afterCommit = true; in PaymentNotification class
Two:
use afterCommit like this:
DB::afterCommit(function () use ($order) {
PaymentNotification ::dispatch($order); // Only runs after successful commit
});
Three:
Dispatch the event outside the DB::transaction block:
but first you need to check if order successfully updated
My recommendation: solution one is the cleanest and most Laravel-idiomatic approach