Skip to content

Lesson 6 of 9 · 23m

Events and Listeners: Decoupling Without Losing the Plot

Domain events as facts, queued listeners, after-commit behavior, and the visibility problems that make event-heavy systems hard to debug.

Events are the sharpest double-edged tool in Laravel architecture. Used well, they let billing, audit logs, notifications, and integrations react to facts without the original action knowing every downstream consumer. Used badly, they turn your application into a haunted forest where saving a model causes six side effects nobody can find.

Events are facts, not commands

final class InvoiceIssued
{
    public function __construct(
        public int $invoiceId,
        public int $customerId,
        public int $totalCents,
    ) {}
}

InvoiceIssued is a fact. SendInvoiceEmail is a command. Facts can have multiple independent consumers; commands belong in jobs or actions. If an event name reads like an instruction, the boundary is already muddy.

Dispatch after the state change is durable:

DB::transaction(function () use ($invoice) {
    $invoice->save();
    InvoiceIssued::dispatch($invoice->id, $invoice->customer_id, $invoice->total_cents);
});

For events that must absolutely not fire when the transaction rolls back, Laravel's afterCommit behavior on events and jobs is the right hook. A notification about an invoice that never committed is worse than a delayed notification.

Listeners should be small and replaceable

final class QueueInvoiceEmail implements ShouldQueue
{
    public function handle(InvoiceIssued $event): void
    {
        SendInvoiceEmailJob::dispatch($event->invoiceId);
    }
}

A listener can perform the work directly or dispatch a dedicated job. Prefer the dedicated job when the work has its own retry, timeout, and idempotency concerns. ShouldQueue moves the listener off the request path, but it does not magically make the work correct.

Discoverability is the tax

In a controller, you can read side effects top to bottom. In an evented system, you must look at the listener registry. Keep it boring and centralized:

protected $listen = [
    InvoiceIssued::class => [
        QueueInvoiceEmail::class,
        RecordInvoiceAnalytics::class,
    ],
];

In modern Laravel, type-hinted listener discovery can reduce registration noise, but implicit discovery makes global reasoning harder in very large applications. Pick a convention and document it where developers actually look.

Model events are a trap for business logic

Order::created(function ...) feels convenient until tests create fixtures, imports create rows, or a background process uses a different path. Business facts belong in explicit application actions. Use model observers for infrastructure concerns like cache invalidation, not core workflows like charging cards.

Field notes

  • Event payloads should be small immutable facts. Passing entire mutable models invites serialization and staleness problems.
  • Do not dispatch events from inside loops during imports unless you want to create a queue flood.
  • A listener failure should not silently block unrelated listeners; queued listeners isolate that blast radius.
  • When debugging, search by event class name. If that search does not reveal all consumers, your event system is under-documented.

Was this lesson helpful?

Sign in to rate