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 41
static void bootHasEvents()

Boot the has event trait for a model.

Return Value

void

at line 57
static array resolveObserveAttributes()

Resolve the observe class names from the attributes.

Return Value

array

at line 88
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 104
protected void registerObserver(object|string $class)

Register a single observer with the model.

Parameters

object|string $class

Return Value

void

Exceptions

InvalidArgumentException

at line 144
array getObservableEvents()

Get the observable event names.

Return Value

array

at line 162
HasEvents setObservableEvents(array $observables)

Set the observable event names.

Parameters

array $observables

Return Value

HasEvents

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

Add an observable event name.

Parameters

array|string $observables

Return Value

void

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

Remove an observable event name.

Parameters

array|string $observables

Return Value

void

at line 200
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 212
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 242
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 260
protected mixed filterModelEventResults(mixed $result)

Filter the model event results.

Parameters

mixed $result

Return Value

mixed

at line 278
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 292
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 306
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 320
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 334
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 348
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 362
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 376
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 390
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 404
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 415
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 437
array dispatchesEvents()

Get the event map for the model.

Return Value

array

at line 448
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 463
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 474
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 490
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 505
static bool eventsDisabled()

Determine if model events are currently disabled for this coroutine.

Return Value

bool