class ModelObserver

Observer that handles search index updates for Eloquent models.

Listens to model events (saved, deleted, forceDeleted, restored) and updates the search index accordingly. Supports soft deletes and transaction-aware event dispatching.

Constants

protected SYNCING_DISABLED_CONTEXT_KEY_PREFIX

Context key prefix for storing whether syncing is disabled per model class.

protected FORCE_SAVING_CONTEXT_KEY

Context key for storing whether the current coroutine is forcing a search update.

Properties

bool $afterCommit

Indicates if Scout will dispatch the observer's events after all database transactions have committed.

protected bool $usingSoftDeletes

Indicates if Scout will keep soft deleted records in the search indexes.

Methods

__construct()

Create a new observer instance.

static void
enableSyncingFor(string $class)

Enable syncing for the given class.

static void
disableSyncingFor(string $class)

Disable syncing for the given class.

static bool
syncingDisabledFor(object|string $class)

Determine if syncing is disabled for the given class or model.

void
saved(Model $model)

Handle the saved event for the model.

void
deleted(Model $model)

Handle the deleted event for the model.

void
forceDeleted(Model $model)

Handle the force deleted event for the model.

void
restored(Model $model)

Handle the restored event for the model.

mixed
whileForcingUpdate(Closure $callback)

Execute the given callback while forcing updates.

bool
usesSoftDelete(Model $model)

Determine if the given model uses soft deletes.

Details

at line 46
__construct()

Create a new observer instance.

at line 59
static void enableSyncingFor(string $class)

Enable syncing for the given class.

Uses Context for coroutine-safe state management.

Parameters

string $class

Return Value

void

at line 71
static void disableSyncingFor(string $class)

Disable syncing for the given class.

Uses Context for coroutine-safe state management.

Parameters

string $class

Return Value

void

at line 83
static bool syncingDisabledFor(object|string $class)

Determine if syncing is disabled for the given class or model.

Uses Context for coroutine-safe state management.

Parameters

object|string $class

Return Value

bool

at line 95
void saved(Model $model)

Handle the saved event for the model.

Parameters

Model $model

Return Value

void

at line 125
void deleted(Model $model)

Handle the deleted event for the model.

Parameters

Model $model

Return Value

void

at line 150
void forceDeleted(Model $model)

Handle the force deleted event for the model.

Parameters

Model $model

Return Value

void

at line 164
void restored(Model $model)

Handle the restored event for the model.

Parameters

Model $model

Return Value

void

at line 174
protected mixed whileForcingUpdate(Closure $callback)

Execute the given callback while forcing updates.

Parameters

Closure $callback

Return Value

mixed

at line 189
protected bool usesSoftDelete(Model $model)

Determine if the given model uses soft deletes.

Parameters

Model $model

Return Value

bool