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 <cursoragent@cursor.com>
This commit is contained in:
Paulo Castellano 2026-07-24 12:17:43 -03:00
parent 254b7d514b
commit 2aef07dde6
2 changed files with 3 additions and 3 deletions

View file

@ -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;

View file

@ -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