Laravel Events and Listeners: A Practical Example with Store Setup
Raheel Shan
August 28th, 2025 · 2 min read
A Laravel event is a class that announces something happened, such as StoreCreated. A listener is a separate class that reacts to it, such as SendStoreReadyEmail. You fire the event once, and Laravel runs every listener attached to it. That lets you split a long process into small, testable classes instead of one oversized controller or service. This post builds a complete example, a store setup with eight steps, and shows the mistakes that make events hard to debug.
Quick answer
- Create an event:
php artisan make:event StoreCreated. - Create one listener per task:
php artisan make:listener AssignStore --event=StoreCreated. - Let Laravel discover the listeners, or register them yourself.
- Fire the event with
StoreCreated::dispatch($store).
The problem: one process, many tasks
Say a user registers and saves a new store in an e-commerce SaaS. Behind the Save button, all of this has to happen:
- Save the store.
- Assign the store to the current user.
- Give the user the admin role.
- Give that admin the default abilities.
- Create default brands, categories and products.
- Email the owner: "Your store is ready."
- Email the super admin: "A new store was registered."
Putting that in a controller makes it unreadable. Putting it in one service class makes a giant transaction that is painful to test. With events, the controller saves the store and announces it. Each step becomes its own class.
Create the event
php artisan make:event StoreCreated
<?php
namespace App\Events;
use App\Models\Store;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class StoreCreated
{
use Dispatchable, SerializesModels;
public function __construct(public Store $store)
{
}
}
The event only carries data. SerializesModels matters once a listener is queued, because the model is stored by its id and reloaded.
Create the listeners
php artisan make:listener AssignStore --event=StoreCreated
php artisan make:listener AssignAdminRole --event=StoreCreated
php artisan make:listener AssignAdminAbilities --event=StoreCreated
php artisan make:listener AddDefaultBrands --event=StoreCreated
php artisan make:listener AddDefaultCategories --event=StoreCreated
php artisan make:listener AddDefaultProducts --event=StoreCreated
php artisan make:listener SendStoreReadyEmail --event=StoreCreated
php artisan make:listener SendNewStoreCreatedEmail --event=StoreCreated
Each listener has one handle() method that type-hints the event:
<?php
namespace App\Listeners;
use App\Events\StoreCreated;
class AddDefaultBrands
{
public function handle(StoreCreated $event): void
{
$event->store->brands()->createMany([
['name' => 'Generic'],
['name' => 'Other'],
]);
}
}
Register the listeners
There are two ways, and you should pick one for each listener.
Option 1: automatic discovery. Laravel 11 and up scans app/Listeners and registers any class whose handle() or __invoke() method type-hints an event. Nothing else to do. You can group related listeners in a folder named after the event, for example app/Listeners/StoreCreated/AssignStore.php.
Option 2: register them yourself. This lets you keep listener classes anywhere, such as services or AQC classes. In AppServiceProvider::boot():
use App\Events\StoreCreated;
use Illuminate\Support\Facades\Event;
public function boot(): void
{
foreach ([
\App\Listeners\AssignStore::class,
\App\Listeners\AssignAdminRole::class,
\App\Listeners\AssignAdminAbilities::class,
\App\Listeners\AddDefaultBrands::class,
\App\Listeners\AddDefaultCategories::class,
\App\Listeners\AddDefaultProducts::class,
\App\Listeners\SendStoreReadyEmail::class,
\App\Listeners\SendNewStoreCreatedEmail::class,
] as $listener) {
Event::listen(StoreCreated::class, $listener);
}
}
Do not use both for the same class. A listener that is discovered and also registered by hand can run twice. In older Laravel versions the same list went into the $listen array of an EventServiceProvider, which a fresh Laravel 11 app no longer has.
Fire the event
Dispatch it after the store is saved. The shortest form is:
StoreCreated::dispatch($store);
I usually do this in a model observer so every code path that creates a store fires it:
<?php
namespace App\Observers;
use App\Events\StoreCreated;
use App\Models\Store;
use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit;
class StoreObserver implements ShouldHandleEventsAfterCommit
{
public function created(Store $store): void
{
StoreCreated::dispatch($store);
}
}
Register it with Store::observe(StoreObserver::class) in a service provider. ShouldHandleEventsAfterCommit makes the observer wait until the database transaction commits, so listeners never see a store that is later rolled back. For more on observers, see Distributed Business Logic with Laravel Observers.
Queue the slow listeners
By default every listener runs in the same request, one after another. Emails and bulk inserts should not make the user wait. Add ShouldQueue and the listener moves to the queue:
<?php
namespace App\Listeners;
use App\Events\StoreCreated;
use App\Mail\StoreReady;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;
class SendStoreReadyEmail implements ShouldQueue
{
public function handle(StoreCreated $event): void
{
Mail::to($event->store->owner)->send(new StoreReady($event->store));
}
}
This needs a running queue worker. Without one, queued listeners wait forever.
Test events and listeners
Small classes are easy to test. Check that the event fires without running any listener by faking it, and test each listener on its own by calling handle().
use App\Events\StoreCreated;
use Illuminate\Support\Facades\Event;
public function test_saving_a_store_fires_the_event(): void
{
Event::fake([StoreCreated::class]);
$store = Store::factory()->create();
Event::assertDispatched(StoreCreated::class, fn ($e) => $e->store->is($store));
}
For a listener, build the event yourself and call (new AddDefaultBrands)->handle(new StoreCreated($store)), then assert on the database.
Limits and mistakes to avoid
- Order and failures: synchronous listeners run in the order they were registered. If one throws an exception, the rest do not run. A listener can also return
falseto stop the ones after it. - Hidden flow: a reader of the controller cannot see what happens next. Name events after what happened (
StoreCreated) and keep the listener list short and in one place. - Dependencies between steps: the admin role must exist before the abilities are assigned. Keep dependent steps in one listener, or queue them in a chain, instead of relying on order.
- Not for everything: a two-step process does not need an event. Use events when a process has several independent reactions that may grow.
- Business rules: if the logic is a query, keep it in a class of its own. See Atomic Query Construction, and Laravel Business Logic: A Cleaner and Smarter Approach for how I split logic.
FAQ
What is the difference between events and observers in Laravel? An observer reacts to Eloquent model changes such as created or updated. An event is a custom announcement you define yourself. I use an observer to fire an event when a model is created.
Where do I register events and listeners in Laravel 11? Automatically: put listeners in app/Listeners and Laravel discovers them. Manually: call Event::listen() in AppServiceProvider::boot(). The EventServiceProvider is no longer part of a fresh app.
Do Laravel listeners run synchronously? Yes, by default, in the same request. Implement ShouldQueue on the listener to run it on the queue.
How do I stop other listeners from running? Return false from handle(). The listeners after it do not run.
Can one listener handle several events? Yes. Add one method per event, or a union type, and register each pairing. Keep it small, or you lose the single-purpose benefit.
Structure your Laravel app with me
Fat controllers, business logic in five places, views that accept any array. Bring your codebase or your plan.
In 60 minutes I review how it is laid out and we settle modules, DTOs, ViewModels and clean controllers.
Found this useful?
Support the work that goes into these posts.