trait Searchable mixin Model

Provides full-text search capabilities to Eloquent models.

Constants

SCOUT_RUNNER_CONTEXT_KEY

Coroutine-local context key for the concurrent 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.

protected SCOUT_JOBS_CONTEXT_KEY

Coroutine-local context key for deferred HTTP indexing jobs.

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|null $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(bool $force = false)

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.

string
searchableEmbeddingColumn()

Get the database column that stores the searchable embedding.

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.

Details

at line 58
static void bootSearchable()

Boot the searchable trait.

Return Value

void

at line 74
void registerSearchableMacros()

Register the searchable macros on collections.

These macros persist for the worker lifetime and affect every subsequent request.

Return Value

void

at line 141
void queueMakeSearchable(Collection $models)

Dispatch the job to make the given models searchable.

Parameters

Collection $models

Return Value

void

at line 175
void syncMakeSearchable(Collection $models)

Synchronously make the given models searchable.

Parameters

Collection $models

Return Value

void

at line 193
void queueRemoveFromSearch(Collection $models)

Dispatch the job to make the given models unsearchable.

Parameters

Collection $models

Return Value

void

at line 220
void syncRemoveFromSearch(Collection $models)

Synchronously make the given models unsearchable.

Parameters

Collection $models

Return Value

void

at line 232
bool shouldBeSearchable()

Determine if the model should be searchable.

Return Value

bool

at line 240
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.

Models may define a protected static class-string $scoutBuilder to select a custom builder.

Parameters

string|null $query
Closure|null $callback

Return Value

Builder

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

Make all instances of the model searchable.

Parameters

int|null $chunk

Return Value

void

at line 276
static Builder makeAllSearchableQuery()

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

Return Value

Builder

at line 293
Collection makeSearchableUsing(Collection $models)

Modify the collection of models being made searchable.

Parameters

Collection $models

Return Value

Collection

at line 301
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 309
void searchable()

Make the given model instance searchable.

Return Value

void

at line 317
void searchableSync()

Synchronously make the given model instance searchable.

Return Value

void

at line 325
static void removeAllFromSearch(bool $force = false)

Remove all instances of the model from the search index.

Parameters

bool $force

Return Value

void

at line 338
void unsearchable()

Remove the given model instance from the search index.

Return Value

void

at line 346
void unsearchableSync()

Synchronously remove the given model instance from the search index.

Return Value

void

at line 354
bool wasSearchableBeforeUpdate()

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

Return Value

bool

at line 362
bool wasSearchableBeforeDelete()

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

Return Value

bool

at line 373
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 384
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 407
static void enableSearchSyncing()

Enable search syncing for this model.

Return Value

void

at line 415
static void disableSearchSyncing()

Disable search syncing for this model.

Return Value

void

at line 423
static bool isSearchSyncingEnabled()

Determine if search syncing is enabled for this model.

Return Value

bool

at line 431
static mixed withoutSyncingToSearch(callable $callback)

Temporarily disable search syncing for the given callback.

Parameters

callable $callback

Return Value

mixed

at line 447
string searchableAs()

Get the index name for the model when searching.

Return Value

string

at line 455
string indexableAs()

Get the index name for the model when indexing.

Return Value

string

at line 463
string searchableEmbeddingColumn()

Get the database column that stores the searchable embedding.

Return Value

string

at line 471
array toSearchableArray()

Get the indexable data array for the model.

Return Value

array

at line 479
Engine searchableUsing()

Get the Scout engine for the model.

Return Value

Engine

at line 487
string|null syncWithSearchUsing()

Get the queue connection that should be used when syncing.

Return Value

string|null

at line 495
string|null syncWithSearchUsingQueue()

Get the queue that should be used with syncing.

Return Value

string|null

at line 505
Searchable pushSoftDeleteMetadata()

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

Return Value

Searchable

at line 513
array scoutMetadata()

Get all Scout related metadata.

Return Value

array

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

Set a Scout related metadata.

Parameters

string $key
mixed $value

Return Value

Searchable

at line 533
mixed getScoutKey()

Get the value used to index the model.

Return Value

mixed

at line 550
string getScoutKeyName()

Get the key name used to index the model.

Return Value

string

at line 558
string getScoutKeyType()

Get the auto-incrementing key type for querying models.

Return Value

string

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

Dispatch the job to scout the given models.

Parameters

callable $job

Return Value

void

at line 618
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 634
static protected bool usesSoftDelete()

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

Return Value

bool