What are events?
Laravel’s event system implements the observer pattern. When something notable happens in your application—an order ships, a user registers—you dispatch an event. One or more listeners respond to that event independently. This decouples your core business logic from side effects like sending emails or updating analytics. You can add, remove, or change listeners without touching the code that raised the event.Generating events and listeners
Use Artisan to scaffold events and listeners:Registering events and listeners
Event discovery (automatic)
By default, Laravel scansapp/Listeners automatically and registers any listener method that begins with handle or __invoke. The event it listens to is determined by the type-hint in the method signature:
Manual registration
Register events manually insideAppServiceProvider::boot using the Event facade:
Closure listeners
Register a closure directly for lightweight, one-off reactions:Defining events
An event is a data container. It holds the information needed by listeners—nothing more:SerializesModels ensures Eloquent models serialize correctly when the event is used with a queued listener.
Defining listeners
Listeners receive the event in theirhandle method. The service container injects any constructor dependencies automatically:
false from handle to stop the event from propagating to other listeners.
Dispatching events
Calldispatch on the event class:
Queued listeners
Listeners that send emails or call external APIs should run in the background. ImplementShouldQueue to push the listener onto the queue automatically:
Queueable closure listeners
Wrap a closure withqueueable to run it on the queue:
Event subscribers
An event subscriber is a single class that handles multiple events. Implement asubscribe method and register the class:
AppServiceProvider::boot:
End-to-end example
Testing events
Fake all events to assert they were dispatched without triggering listeners:- Pest
- PHPUnit
Queues
Learn how to run queued listeners and background jobs at scale.