trait HasEvents

Properties

protected array<string, class-string> $dispatchesEvents

The event map for the model.

protected string[] $observables

User exposed observable events.

Methods

static void
bootHasEvents()

Boot the has event trait for a model.

static array
resolveObserveAttributes()

Resolve the observe class names from the attributes.

static void
observe(object|array|string $classes)

Register observers with the model.

void
registerObserver(object|string $class)

Register a single observer with the model.

array
getObservableEvents()

Get the observable event names.

setObservableEvents(array $observables)

Set the observable event names.

void
addObservableEvents(array|string $observables)

Add an observable event name.

void
removeObservableEvents(array|string $observables)

Remove an observable event name.

static void
registerModelEvent(string $event, mixed $callback)

Register a model event with the dispatcher.

mixed
fireModelEvent(string $event, bool $halt = true)

Fire the given event for the model.

mixed
fireCustomModelEvent(string $event, string $method)

Fire a custom model event for the given event.

mixed
filterModelEventResults(mixed $result)

Filter the model event results.

static void
retrieved(mixed $callback)

Register a retrieved model event with the dispatcher.

static void
saving(mixed $callback)

Register a saving model event with the dispatcher.

static void
saved(mixed $callback)

Register a saved model event with the dispatcher.

static void
updating(mixed $callback)

Register an updating model event with the dispatcher.

static void
updated(mixed $callback)

Register an updated model event with the dispatcher.

static void
creating(mixed $callback)

Register a creating model event with the dispatcher.

static void
created(mixed $callback)

Register a created model event with the dispatcher.

static void
replicating(mixed $callback)

Register a replicating model event with the dispatcher.

static void
deleting(mixed $callback)

Register a deleting model event with the dispatcher.

static void
deleted(mixed $callback)

Register a deleted model event with the dispatcher.

static void
flushEventListeners()

Remove all the event listeners for the model.

array
dispatchesEvents()

Get the event map for the model.

static Dispatcher|null
getEventDispatcher()

Get the event dispatcher instance.

static void
setEventDispatcher(Dispatcher $dispatcher)

Set the event dispatcher instance.

static void
unsetEventDispatcher()

Unset the event dispatcher for models.

static mixed
withoutEvents(callable $callback)

Execute a callback without firing any model events for any model type.

static bool
eventsDisabled()

Determine if model events are currently disabled for this coroutine.

Details

at line 40
static void bootHasEvents()

Boot the has event trait for a model.

Return Value

void

at line 56
static array resolveObserveAttributes()

Resolve the observe class names from the attributes.

Return Value

array

at line 87
static void observe(object|array|string $classes)

Register observers with the model.

Boot-only. Observer listeners are registered on the worker-wide event dispatcher and persist for the worker lifetime; runtime use leaks listeners across every subsequent request.

Parameters

object|array|string $classes

Return Value

void

Exceptions

InvalidArgumentException

at line 103
protected void registerObserver(object|string $class)

Register a single observer with the model.

Parameters

object|string $class

Return Value

void

Exceptions

InvalidArgumentException

at line 143
array getObservableEvents()

Get the observable event names.

Return Value

array

at line 161
HasEvents setObservableEvents(array $observables)

Set the observable event names.

Parameters

array $observables

Return Value

HasEvents

at line 173
void addObservableEvents(array|string $observables)

Add an observable event name.

Parameters

array|string $observables

Return Value

void

at line 186
void removeObservableEvents(array|string $observables)

Remove an observable event name.

Parameters

array|string $observables

Return Value

void

at line 199
static protected void registerModelEvent(string $event, mixed $callback)

Register a model event with the dispatcher.

Parameters

string $event
mixed $callback

Return Value

void

at line 211
protected mixed fireModelEvent(string $event, bool $halt = true)

Fire the given event for the model.

Parameters

string $event
bool $halt

Return Value

mixed

at line 241
protected mixed fireCustomModelEvent(string $event, string $method)

Fire a custom model event for the given event.

Parameters

string $event
string $method

Return Value

mixed

at line 259
protected mixed filterModelEventResults(mixed $result)

Filter the model event results.

Parameters

mixed $result

Return Value

mixed

at line 277
static void retrieved(mixed $callback)

Register a retrieved model event with the dispatcher.

Boot-only. Listener persists on the worker-wide event dispatcher for the worker lifetime; runtime use leaks listeners across every subsequent request.

Parameters

mixed $callback

Return Value

void

at line 291
static void saving(mixed $callback)

Register a saving model event with the dispatcher.

Boot-only. Listener persists on the worker-wide event dispatcher for the worker lifetime; runtime use leaks listeners across every subsequent request.

Parameters

mixed $callback

Return Value

void

at line 305
static void saved(mixed $callback)

Register a saved model event with the dispatcher.

Boot-only. Listener persists on the worker-wide event dispatcher for the worker lifetime; runtime use leaks listeners across every subsequent request.

Parameters

mixed $callback

Return Value

void

at line 319
static void updating(mixed $callback)

Register an updating model event with the dispatcher.

Boot-only. Listener persists on the worker-wide event dispatcher for the worker lifetime; runtime use leaks listeners across every subsequent request.

Parameters

mixed $callback

Return Value

void

at line 333
static void updated(mixed $callback)

Register an updated model event with the dispatcher.

Boot-only. Listener persists on the worker-wide event dispatcher for the worker lifetime; runtime use leaks listeners across every subsequent request.

Parameters

mixed $callback

Return Value

void

at line 347
static void creating(mixed $callback)

Register a creating model event with the dispatcher.

Boot-only. Listener persists on the worker-wide event dispatcher for the worker lifetime; runtime use leaks listeners across every subsequent request.

Parameters

mixed $callback

Return Value

void

at line 361
static void created(mixed $callback)

Register a created model event with the dispatcher.

Boot-only. Listener persists on the worker-wide event dispatcher for the worker lifetime; runtime use leaks listeners across every subsequent request.

Parameters

mixed $callback

Return Value

void

at line 375
static void replicating(mixed $callback)

Register a replicating model event with the dispatcher.

Boot-only. Listener persists on the worker-wide event dispatcher for the worker lifetime; runtime use leaks listeners across every subsequent request.

Parameters

mixed $callback

Return Value

void

at line 389
static void deleting(mixed $callback)

Register a deleting model event with the dispatcher.

Boot-only. Listener persists on the worker-wide event dispatcher for the worker lifetime; runtime use leaks listeners across every subsequent request.

Parameters

mixed $callback

Return Value

void

at line 403
static void deleted(mixed $callback)

Register a deleted model event with the dispatcher.

Boot-only. Listener persists on the worker-wide event dispatcher for the worker lifetime; runtime use leaks listeners across every subsequent request.

Parameters

mixed $callback

Return Value

void

at line 414
static void flushEventListeners()

Remove all the event listeners for the model.

Boot or tests only. Removes listeners from the worker-wide event dispatcher; concurrent coroutines lose model events mid-request.

Return Value

void

at line 436
array dispatchesEvents()

Get the event map for the model.

Return Value

array

at line 447
static Dispatcher|null getEventDispatcher()

Get the event dispatcher instance.

Returns a NullDispatcher when events are disabled (inside withoutEvents()) to ensure manual dispatch() calls are also suppressed, matching Laravel behavior.

Return Value

Dispatcher|null

at line 462
static void setEventDispatcher(Dispatcher $dispatcher)

Set the event dispatcher instance.

Boot-only. The dispatcher persists in a static property for the worker lifetime and is used by every model event firing across all coroutines.

Parameters

Dispatcher $dispatcher

Return Value

void

at line 473
static void unsetEventDispatcher()

Unset the event dispatcher for models.

Tests only. Clears the worker-wide event dispatcher shared by every coroutine; concurrent model operations will not fire events.

Return Value

void

at line 489
static mixed withoutEvents(callable $callback)

Execute a callback without firing any model events for any model type.

Uses Context for coroutine-safe event disabling, ensuring concurrent requests don't interfere with each other's event handling.

Parameters

callable $callback

Return Value

mixed

at line 504
static bool eventsDisabled()

Determine if model events are currently disabled for this coroutine.

Return Value

bool