What are model events?
Eloquent models automatically fire events at each stage of their lifecycle. You can hook into these events to run code before or after a model is saved, deleted, and so on.
Events ending in
-ing fire before the change is persisted. Events ending in -ed fire after.
Closure-based event listeners
For simple cases, register closures inside the model’sbooted method.
queueable helper.
The $dispatchesEvents property
When you want model events to feed into Laravel’s event system, map them to dedicated event classes using$dispatchesEvents.
Creating an Observer class
When a model triggers several events, an Observer class is cleaner than scattered closures.1
Generate the Observer with Artisan
Use This creates
make:observer with the --model option to get stubs for the relevant event methods.app/Observers/UserObserver.php.2
Implement the event methods
Each method name corresponds to an event. The method receives the model instance.
3
Register the Observer
There are two ways to register an observer. The To attach multiple observers, repeat the attribute or pass an array.Option 2: Register in AppServiceProvider
#[ObservedBy] attribute is preferred in Laravel 13.Option 1: #[ObservedBy] attribute (recommended)Add the attribute to the model class. No changes to AppServiceProvider are needed.The
#[ObservedBy] attribute lives in the Illuminate\Database\Eloquent\Attributes namespace. It is a native PHP 8.0+ attribute and the approach encouraged in Laravel 13.Running Observers after a transaction commits
When a model is created or updated inside a database transaction, you may want the observer to run only after the transaction commits successfully. ImplementShouldHandleEventsAfterCommit to get this behavior.
Temporarily disabling events
withoutEvents — silence all events for a block of code
Inside the closure passed towithoutEvents(), no model events fire.
saveQuietly — save without firing events
Practical use cases
Clearing cache automatically
Recording an audit log
getDirty() returns the attributes that have changed since the model was last synced.
Updating related models automatically
Next steps
Eloquent scopes
Learn how local and global scopes let you reuse query constraints across your application.