Skip to content

Event Traits ​

Four traits are mixed into every ModelEvent subclass automatically. They populate contextual data — who triggered the event, what changed, where the request came from, and what state the model transitioned through — so listeners never have to repeat that boilerplate.

Trait Summary ​

TraitFileWhat it captures
EventUserEvents/Traits/EventUser.phpAuthenticated user at fire time
EventUrlsEvents/Traits/EventUrls.phpCurrent and previous HTTP URLs
EventChangesEvents/Traits/EventChanges.phpDirty attributes and changed relationships
EventStateableEvents/Traits/EventStateable.phpState machine transition details

Setup Lifecycle ​

ModelEvent::__construct() calls each trait's setup method in this order:

new SomeModelEvent($model)
    │
    ├─ setupEventUser()       → $this->user
    ├─ setupEventUrls()       → $this->recentUrl, $this->previousUrl
    ├─ setupEventChanges()    → $this->changedAttributes, $this->changedRelationships
    └─ setupEventStateable()  → $this->hasStateable, $this->stateableChanged, ...

All properties are set by the time any listener receives the event.

Using Traits in a Listener ​

php
public function handle(SomeModelEvent $event): void
{
    // EventUser
    if ($event->hasUser()) {
        $userId = $event->getUser()->id;
    }

    // EventUrls
    $from = $event->getPreviousUrl();
    $to   = $event->getRecentUrl();

    // EventChanges
    if ($event->wasChanged('status')) {
        // status attribute or relationship changed
    }

    // EventStateable
    if ($event->stateableChanged) {
        $transition = $event->previousStateableState . ' → ' . $event->currentStateableState;
    }
}