class Builder

Fluent search query builder for searchable models.

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
SearchableInterface&TModel $model

The model instance.

string $query

The query expression.

Closure|null $callback

Optional callback before search execution.

Closure|null $queryCallback

Optional callback before model query execution.

Closure|null $afterRawSearchCallback

Optional callback after raw search.

string|null $index

The custom index specified for the search.

array<int, array{field: string, operator: string, value: mixed}> $wheres

The "where" constraints added to the query.

array<string, mixed[]> $whereIns

The "where in" constraints added to the query.

array<string, mixed[]> $whereNotIns

The "where not in" constraints added to the query.

int|null $limit

The "limit" that should be applied to the search.

array<array{column: string, direction: string}> $orders

The "order" that should be applied to the search.

bool $semanticSearch

Indicates that the query should use semantic search.

float|int|null $minimumSimilarity

The minimum similarity for semantic search results.

null|array{text_weight: float|int, semantic_weight: float|int} $hybridSearch

The hybrid search ranking weights.

array<string, mixed> $options

Extra options that should be applied to the search.

Methods

mixed
when(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

mixed
unless(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

static void
macro(string $name, callable|object $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Check if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
tap(callable|null $callback = null)

Call the given Closure with this instance then return the instance.

from  Tappable
__construct(Model $model, string|null $query, Closure|null $callback = null, bool $softDelete = false)

Create a new search builder instance.

within(string $index)

Specify a custom index for search or filtered deletion.

where(string $field, mixed $operator, mixed $value = null)

Add a constraint to the search query.

whereIn(string $field, array|Arrayable $values)

Add a "where in" constraint to the search query.

whereNotIn(string $field, array|Arrayable $values)

Add a "where not in" constraint to the search query.

withTrashed()

Include soft deleted records in the results.

onlyTrashed()

Include only soft deleted records in the results.

take(int $limit)

Set the "limit" for the search query.

orderBy(string $column, string $direction = 'asc')

Add an "order" for the search query.

orderByDesc(string $column)

Add a descending "order by" clause to the search query.

latest(string|null $column = null)

Add an "order by" clause for a timestamp to the query (descending).

oldest(string|null $column = null)

Add an "order by" clause for a timestamp to the query (ascending).

semantic(float|int|null $minSimilarity = null)

Perform a semantic search for the query expression.

hybrid(float|int $textWeight = 1, float|int $semanticWeight = 1, float|int|null $minSimilarity = null)

Perform a hybrid full-text and semantic search for the query expression.

options(array $options)

Set extra options for the search query.

query(callable $callback)

Set the callback that should have an opportunity to modify the database query.

mixed
raw()

Get the raw results of the search.

withRawResults(callable $callback)

Set the callback that should have an opportunity to inspect and modify the raw result returned by the search engine.

keys()

Get the keys of search results.

Model|null
first()

Get the first result from the search.

get()

Get the results of the search.

cursor()

Get the results of the search as a lazy collection.

simplePaginate(int|null $perPage = null, string $pageName = 'page', int|null $page = null)

Paginate the given query into a simple paginator.

paginate(int|null $perPage = null, string $pageName = 'page', int|null $page = null)

Paginate the given query into a length-aware paginator.

paginateRaw(int|null $perPage = null, string $pageName = 'page', int|null $page = null)

Paginate the given query into a length-aware paginator with raw data.

simplePaginateRaw(int|null $perPage = null, string $pageName = 'page', int|null $page = null)

Paginate the given query into a simple paginator with raw data.

int
getTotalCount(mixed $results)

Get the total number of results from the Scout engine, or fallback to query builder.

mixed
applyAfterRawSearchCallback(mixed $results)

Invoke the "after raw search" callback.

preparedEngine()

Get the prepared engine that should handle the query.

engine()

Get the engine that should handle the query.

string
modelConnectionType()

Get the connection type for the underlying model.

static void
flushState()

Flush all static state.

Details

in Conditionable at line 23
mixed when(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

Parameters

mixed $value
callable|null $callback
callable|null $default

Return Value

mixed

in Conditionable at line 56
mixed unless(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

Parameters

mixed $value
callable|null $callback
callable|null $default

Return Value

mixed

in Macroable at line 30
static void macro(string $name, callable|object $macro)

Register a custom macro.

Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.

Parameters

string $name
callable|object $macro

Return Value

void

in Macroable at line 44
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 61
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 72
static void flushMacros()

Flush the existing macros.

Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.

Return Value

void

in Macroable at line 83
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in Macroable at line 108
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in Tappable at line 17
mixed tap(callable|null $callback = null)

Call the given Closure with this instance then return the instance.

Parameters

callable|null $callback

Return Value

mixed

at line 135
__construct(Model $model, string|null $query, Closure|null $callback = null, bool $softDelete = false)

Create a new search builder instance.

Parameters

Model $model
string|null $query
Closure|null $callback
bool $softDelete

at line 160
Builder within(string $index)

Specify a custom index for search or filtered deletion.

Parameters

string $index

Return Value

Builder

at line 172
Builder where(string $field, mixed $operator, mixed $value = null)

Add a constraint to the search query.

Parameters

string $field
mixed $operator
mixed $value

Return Value

Builder

at line 192
Builder whereIn(string $field, array|Arrayable $values)

Add a "where in" constraint to the search query.

Parameters

string $field
array|Arrayable $values

Return Value

Builder

at line 209
Builder whereNotIn(string $field, array|Arrayable $values)

Add a "where not in" constraint to the search query.

Parameters

string $field
array|Arrayable $values

Return Value

Builder

at line 225
Builder withTrashed()

Include soft deleted records in the results.

Return Value

Builder

at line 240
Builder onlyTrashed()

Include only soft deleted records in the results.

Return Value

Builder

at line 256
Builder take(int $limit)

Set the "limit" for the search query.

Parameters

int $limit

Return Value

Builder

at line 268
Builder orderBy(string $column, string $direction = 'asc')

Add an "order" for the search query.

Parameters

string $column
string $direction

Return Value

Builder

at line 283
Builder orderByDesc(string $column)

Add a descending "order by" clause to the search query.

Parameters

string $column

Return Value

Builder

at line 293
Builder latest(string|null $column = null)

Add an "order by" clause for a timestamp to the query (descending).

Parameters

string|null $column

Return Value

Builder

at line 305
Builder oldest(string|null $column = null)

Add an "order by" clause for a timestamp to the query (ascending).

Parameters

string|null $column

Return Value

Builder

at line 317
Builder semantic(float|int|null $minSimilarity = null)

Perform a semantic search for the query expression.

Parameters

float|int|null $minSimilarity

Return Value

Builder

at line 335
Builder hybrid(float|int $textWeight = 1, float|int $semanticWeight = 1, float|int|null $minSimilarity = null)

Perform a hybrid full-text and semantic search for the query expression.

Parameters

float|int $textWeight
float|int $semanticWeight
float|int|null $minSimilarity

Return Value

Builder

at line 365
Builder options(array $options)

Set extra options for the search query.

Parameters

array $options

Return Value

Builder

at line 377
Builder query(callable $callback)

Set the callback that should have an opportunity to modify the database query.

Parameters

callable $callback

Return Value

Builder

at line 387
mixed raw()

Get the raw results of the search.

Return Value

mixed

at line 398
Builder withRawResults(callable $callback)

Set the callback that should have an opportunity to inspect and modify the raw result returned by the search engine.

Parameters

callable $callback

Return Value

Builder

at line 408
Collection keys()

Get the keys of search results.

Return Value

Collection

at line 418
Model|null first()

Get the first result from the search.

Return Value

Model|null

at line 428
Collection get()

Get the results of the search.

Return Value

Collection

at line 438
LazyCollection cursor()

Get the results of the search as a lazy collection.

Return Value

LazyCollection

at line 446
Paginator simplePaginate(int|null $perPage = null, string $pageName = 'page', int|null $page = null)

Paginate the given query into a simple paginator.

Parameters

int|null $perPage
string $pageName
int|null $page

Return Value

Paginator

at line 499
LengthAwarePaginator paginate(int|null $perPage = null, string $pageName = 'page', int|null $page = null)

Paginate the given query into a length-aware paginator.

Parameters

int|null $perPage
string $pageName
int|null $page

Return Value

LengthAwarePaginator

at line 551
LengthAwarePaginator paginateRaw(int|null $perPage = null, string $pageName = 'page', int|null $page = null)

Paginate the given query into a length-aware paginator with raw data.

Parameters

int|null $perPage
string $pageName
int|null $page

Return Value

LengthAwarePaginator

at line 596
Paginator simplePaginateRaw(int|null $perPage = null, string $pageName = 'page', int|null $page = null)

Paginate the given query into a simple paginator with raw data.

Parameters

int|null $perPage
string $pageName
int|null $page

Return Value

Paginator

at line 643
protected int getTotalCount(mixed $results)

Get the total number of results from the Scout engine, or fallback to query builder.

Parameters

mixed $results

Return Value

int

at line 672
mixed applyAfterRawSearchCallback(mixed $results)

Invoke the "after raw search" callback.

Parameters

mixed $results

Return Value

mixed

at line 684
protected Engine preparedEngine()

Get the prepared engine that should handle the query.

Return Value

Engine

at line 701
protected Engine engine()

Get the engine that should handle the query.

Return Value

Engine

at line 709
string modelConnectionType()

Get the connection type for the underlying model.

Return Value

string

at line 720
static void flushState()

Flush all static state.

Return Value

void