trait Searchable mixin Model

Provides full-text search capabilities to Eloquent models.

Constants

SCOUT_RUNNER_CONTEXT_KEY

Coroutine-local context key for the WaitConcurrent runner used during imports.

Coroutine-local rather than a static property so concurrent coroutines in the same process don't share or overwrite each other's runner instances.

Properties

protected array<string, mixed> $scoutMetadata

Additional metadata attributes managed by Scout.

Methods

static void
bootSearchable()

Boot the searchable trait.

void
registerSearchableMacros()

Register the searchable macros on collections.

void
queueMakeSearchable(Collection $models)

Dispatch the job to make the given models searchable.

void
syncMakeSearchable(Collection $models)

Synchronously make the given models searchable.

void
queueRemoveFromSearch(Collection $models)

Dispatch the job to make the given models unsearchable.

void
syncRemoveFromSearch(Collection $models)

Synchronously make the given models unsearchable.

bool
shouldBeSearchable()

Determine if the model should be searchable.

bool
searchIndexShouldBeUpdated()

When updating a model, this method determines if we should update the search index.

static Builder
search(string $query = '', Closure|null $callback = null)

Perform a search against the model's indexed data.

static void
makeAllSearchable(int|null $chunk = null)

Make all instances of the model searchable.

static Builder
makeAllSearchableQuery()

Get a query builder for making all instances of the model searchable.

makeSearchableUsing(Collection $models)

Modify the collection of models being made searchable.

makeAllSearchableUsing(Builder $query)

Modify the query used to retrieve models when making all of the models searchable.

void
searchable()

Make the given model instance searchable.

void
searchableSync()

Synchronously make the given model instance searchable.

static void
removeAllFromSearch()

Remove all instances of the model from the search index.

void
unsearchable()

Remove the given model instance from the search index.

void
unsearchableSync()

Synchronously remove the given model instance from the search index.

bool
wasSearchableBeforeUpdate()

Determine if the model existed in the search index prior to an update.

bool
wasSearchableBeforeDelete()

Determine if the model existed in the search index prior to deletion.

getScoutModelsByIds(Builder $builder, array $ids)

Get the requested models from an array of object IDs.

queryScoutModelsByIds(Builder $builder, array $ids)

Get a query builder for retrieving the requested models from an array of object IDs.

static void
enableSearchSyncing()

Enable search syncing for this model.

static void
disableSearchSyncing()

Disable search syncing for this model.

static bool
isSearchSyncingEnabled()

Determine if search syncing is enabled for this model.

static mixed
withoutSyncingToSearch(callable $callback)

Temporarily disable search syncing for the given callback.

string
searchableAs()

Get the index name for the model when searching.

string
indexableAs()

Get the index name for the model when indexing.

array
toSearchableArray()

Get the indexable data array for the model.

searchableUsing()

Get the Scout engine for the model.

string|null
syncWithSearchUsing()

Get the queue connection that should be used when syncing.

string|null
syncWithSearchUsingQueue()

Get the queue that should be used with syncing.

pushSoftDeleteMetadata()

Sync the soft deleted status for this model into the metadata.

array
scoutMetadata()

Get all Scout related metadata.

withScoutMetadata(string $key, mixed $value)

Set a Scout related metadata.

mixed
getScoutKey()

Get the value used to index the model.

string
getScoutKeyName()

Get the key name used to index the model.

string
getScoutKeyType()

Get the auto-incrementing key type for querying models.

static void
dispatchSearchableJob(callable $job)

Dispatch the job to scout the given models.

static void
waitForSearchableJobs()

Wait for all pending searchable jobs to complete.

static bool
usesSoftDelete()

Determine if the current class should use soft deletes with searching.

static mixed
getScoutConfig(string $key, mixed $default = null)

Get a Scout configuration value.

Details

at line 43
static void bootSearchable()

Boot the searchable trait.

Return Value

void

at line 57
void registerSearchableMacros()

Register the searchable macros on collections.

Return Value

void

at line 91
void queueMakeSearchable(Collection $models)

Dispatch the job to make the given models searchable.

Parameters

Collection $models

Return Value

void

at line 118
void syncMakeSearchable(Collection $models)

Synchronously make the given models searchable.

Parameters

Collection $models

Return Value

void

at line 136
void queueRemoveFromSearch(Collection $models)

Dispatch the job to make the given models unsearchable.

Parameters

Collection $models

Return Value

void

at line 163
void syncRemoveFromSearch(Collection $models)

Synchronously make the given models unsearchable.

Parameters

Collection $models

Return Value

void

at line 175
bool shouldBeSearchable()

Determine if the model should be searchable.

Return Value

bool

at line 183
bool searchIndexShouldBeUpdated()

When updating a model, this method determines if we should update the search index.

Return Value

bool

Perform a search against the model's indexed data.

Parameters

string $query
Closure|null $callback

Return Value

Builder

at line 206
static void makeAllSearchable(int|null $chunk = null)

Make all instances of the model searchable.

Parameters

int|null $chunk

Return Value

void

at line 214
static Builder makeAllSearchableQuery()

Get a query builder for making all instances of the model searchable.

Return Value

Builder

at line 231
Collection makeSearchableUsing(Collection $models)

Modify the collection of models being made searchable.

Parameters

Collection $models

Return Value

Collection

at line 239
protected Builder makeAllSearchableUsing(Builder $query)

Modify the query used to retrieve models when making all of the models searchable.

Parameters

Builder $query

Return Value

Builder

at line 247
void searchable()

Make the given model instance searchable.

Return Value

void

at line 255
void searchableSync()

Synchronously make the given model instance searchable.

Return Value

void

at line 263
static void removeAllFromSearch()

Remove all instances of the model from the search index.

Return Value

void

at line 272
void unsearchable()

Remove the given model instance from the search index.

Return Value

void

at line 280
void unsearchableSync()

Synchronously remove the given model instance from the search index.

Return Value

void

at line 288
bool wasSearchableBeforeUpdate()

Determine if the model existed in the search index prior to an update.

Return Value

bool

at line 296
bool wasSearchableBeforeDelete()

Determine if the model existed in the search index prior to deletion.

Return Value

bool

at line 304
Collection getScoutModelsByIds(Builder $builder, array $ids)

Get the requested models from an array of object IDs.

Parameters

Builder $builder
array $ids

Return Value

Collection

at line 312
Builder queryScoutModelsByIds(Builder $builder, array $ids)

Get a query builder for retrieving the requested models from an array of object IDs.

Parameters

Builder $builder
array $ids

Return Value

Builder

at line 335
static void enableSearchSyncing()

Enable search syncing for this model.

Return Value

void

at line 343
static void disableSearchSyncing()

Disable search syncing for this model.

Return Value

void

at line 351
static bool isSearchSyncingEnabled()

Determine if search syncing is enabled for this model.

Return Value

bool

at line 359
static mixed withoutSyncingToSearch(callable $callback)

Temporarily disable search syncing for the given callback.

Parameters

callable $callback

Return Value

mixed

at line 373
string searchableAs()

Get the index name for the model when searching.

Return Value

string

at line 381
string indexableAs()

Get the index name for the model when indexing.

Return Value

string

at line 389
array toSearchableArray()

Get the indexable data array for the model.

Return Value

array

at line 397
Engine searchableUsing()

Get the Scout engine for the model.

Return Value

Engine

at line 405
string|null syncWithSearchUsing()

Get the queue connection that should be used when syncing.

Return Value

string|null

at line 413
string|null syncWithSearchUsingQueue()

Get the queue that should be used with syncing.

Return Value

string|null

at line 423
Searchable pushSoftDeleteMetadata()

Sync the soft deleted status for this model into the metadata.

Return Value

Searchable

at line 431
array scoutMetadata()

Get all Scout related metadata.

Return Value

array

at line 441
Searchable withScoutMetadata(string $key, mixed $value)

Set a Scout related metadata.

Parameters

string $key
mixed $value

Return Value

Searchable

at line 451
mixed getScoutKey()

Get the value used to index the model.

Return Value

mixed

at line 459
string getScoutKeyName()

Get the key name used to index the model.

Return Value

string

at line 467
string getScoutKeyType()

Get the auto-incrementing key type for querying models.

Return Value

string

at line 475
static protected void dispatchSearchableJob(callable $job)

Dispatch the job to scout the given models.

Parameters

callable $job

Return Value

void

at line 502
static void waitForSearchableJobs()

Wait for all pending searchable jobs to complete.

Should be called at the end of Scout commands to ensure all concurrent indexing operations have finished.

Return Value

void

at line 515
static protected bool usesSoftDelete()

Determine if the current class should use soft deletes with searching.

Return Value

bool

at line 523
static protected mixed getScoutConfig(string $key, mixed $default = null)

Get a Scout configuration value.

Parameters

string $key
mixed $default

Return Value

mixed