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 $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.

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 57
static void bootSearchable()

Boot the searchable trait.

Return Value

void

at line 73
void registerSearchableMacros()

Register the searchable macros on collections.

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

Return Value

void

at line 139
void queueMakeSearchable(Collection $models)

Dispatch the job to make the given models searchable.

Parameters

Collection $models

Return Value

void

at line 166
void syncMakeSearchable(Collection $models)

Synchronously make the given models searchable.

Parameters

Collection $models

Return Value

void

at line 184
void queueRemoveFromSearch(Collection $models)

Dispatch the job to make the given models unsearchable.

Parameters

Collection $models

Return Value

void

at line 211
void syncRemoveFromSearch(Collection $models)

Synchronously make the given models unsearchable.

Parameters

Collection $models

Return Value

void

at line 223
bool shouldBeSearchable()

Determine if the model should be searchable.

Return Value

bool

at line 231
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 $query
Closure|null $callback

Return Value

Builder

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

Make all instances of the model searchable.

Parameters

int|null $chunk

Return Value

void

at line 267
static Builder makeAllSearchableQuery()

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

Return Value

Builder

at line 284
Collection makeSearchableUsing(Collection $models)

Modify the collection of models being made searchable.

Parameters

Collection $models

Return Value

Collection

at line 292
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 300
void searchable()

Make the given model instance searchable.

Return Value

void

at line 308
void searchableSync()

Synchronously make the given model instance searchable.

Return Value

void

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

Remove all instances of the model from the search index.

Parameters

bool $force

Return Value

void

at line 329
void unsearchable()

Remove the given model instance from the search index.

Return Value

void

at line 337
void unsearchableSync()

Synchronously remove the given model instance from the search index.

Return Value

void

at line 345
bool wasSearchableBeforeUpdate()

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

Return Value

bool

at line 353
bool wasSearchableBeforeDelete()

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

Return Value

bool

at line 364
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 375
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 398
static void enableSearchSyncing()

Enable search syncing for this model.

Return Value

void

at line 406
static void disableSearchSyncing()

Disable search syncing for this model.

Return Value

void

at line 414
static bool isSearchSyncingEnabled()

Determine if search syncing is enabled for this model.

Return Value

bool

at line 422
static mixed withoutSyncingToSearch(callable $callback)

Temporarily disable search syncing for the given callback.

Parameters

callable $callback

Return Value

mixed

at line 438
string searchableAs()

Get the index name for the model when searching.

Return Value

string

at line 446
string indexableAs()

Get the index name for the model when indexing.

Return Value

string

at line 454
array toSearchableArray()

Get the indexable data array for the model.

Return Value

array

at line 462
Engine searchableUsing()

Get the Scout engine for the model.

Return Value

Engine

at line 470
string|null syncWithSearchUsing()

Get the queue connection that should be used when syncing.

Return Value

string|null

at line 478
string|null syncWithSearchUsingQueue()

Get the queue that should be used with syncing.

Return Value

string|null

at line 488
Searchable pushSoftDeleteMetadata()

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

Return Value

Searchable

at line 496
array scoutMetadata()

Get all Scout related metadata.

Return Value

array

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

Set a Scout related metadata.

Parameters

string $key
mixed $value

Return Value

Searchable

at line 516
mixed getScoutKey()

Get the value used to index the model.

Return Value

mixed

at line 533
string getScoutKeyName()

Get the key name used to index the model.

Return Value

string

at line 541
string getScoutKeyType()

Get the auto-incrementing key type for querying models.

Return Value

string

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

Dispatch the job to scout the given models.

Parameters

callable $job

Return Value

void

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

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

Return Value

bool