From 2aef07dde6f57a45e3f315b8b9fbd15e7f83f5f5 Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Fri, 24 Jul 2026 12:17:43 -0300 Subject: [PATCH] Defer PostCreated with DB::afterCommit in the observer. Keep after-commit local to created(), matching the saved() job hook, instead of marking the event itself. Co-authored-by: Cursor --- app/Events/PostCreated.php | 3 +-- app/Observers/PostObserver.php | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Events/PostCreated.php b/app/Events/PostCreated.php index 6234ba0e..013f20d5 100644 --- a/app/Events/PostCreated.php +++ b/app/Events/PostCreated.php @@ -8,11 +8,10 @@ use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; -use Illuminate\Contracts\Events\ShouldDispatchAfterCommit; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\SerializesModels; -class PostCreated implements ShouldBroadcast, ShouldDispatchAfterCommit +class PostCreated implements ShouldBroadcast { use Dispatchable, InteractsWithSockets, SerializesModels; diff --git a/app/Observers/PostObserver.php b/app/Observers/PostObserver.php index be6a3e6c..1486cfc0 100644 --- a/app/Observers/PostObserver.php +++ b/app/Observers/PostObserver.php @@ -9,12 +9,13 @@ use App\Events\PostCreated; use App\Jobs\Automation\DispatchPostTriggerAutomationsJob; use App\Models\Post; +use Illuminate\Support\Facades\DB; class PostObserver { public function created(Post $post): void { - PostCreated::dispatch($post); + DB::afterCommit(fn () => PostCreated::dispatch($post)); } public function saved(Post $post): void