class Builder

Fluent search query builder for searchable models.

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
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<string, 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.

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 $query, Closure|null $callback = null, bool $softDelete = false)

Create a new search builder instance.

within(string $index)

Specify a custom index to perform this search on.

where(string $field, mixed $value)

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

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.

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 28
static void macro(string $name, callable|object $macro)

Register a custom macro.

Boot-only. 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 41
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Boot-only. 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 57
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 68
static void flushMacros()

Flush the existing macros.

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

Return Value

void

in Macroable at line 78
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 103
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 15
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 114
__construct(Model $model, string $query, Closure|null $callback = null, bool $softDelete = false)

Create a new search builder instance.

Parameters

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

at line 134
Builder within(string $index)

Specify a custom index to perform this search on.

Parameters

string $index

Return Value

Builder

at line 146
Builder where(string $field, mixed $value)

Add a constraint to the search query.

Parameters

string $field
mixed $value

Return Value

Builder

at line 159
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 176
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 192
Builder withTrashed()

Include soft deleted records in the results.

Return Value

Builder

at line 204
Builder onlyTrashed()

Include only soft deleted records in the results.

Return Value

Builder

at line 216
Builder take(int $limit)

Set the "limit" for the search query.

Parameters

int $limit

Return Value

Builder

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

Add an "order" for the search query.

Parameters

string $column
string $direction

Return Value

Builder

at line 243
Builder orderByDesc(string $column)

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

Parameters

string $column

Return Value

Builder

at line 253
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 265
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 278
Builder options(array $options)

Set extra options for the search query.

Parameters

array $options

Return Value

Builder

at line 290
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 300
mixed raw()

Get the raw results of the search.

Return Value

mixed

at line 311
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 321
Collection keys()

Get the keys of search results.

Return Value

Collection

at line 331
Model|null first()

Get the first result from the search.

Return Value

Model|null

at line 341
Collection get()

Get the results of the search.

Return Value

Collection

at line 351
LazyCollection cursor()

Get the results of the search as a lazy collection.

Return Value

LazyCollection

at line 359
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 397
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 439
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 468
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 494
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 523
mixed applyAfterRawSearchCallback(mixed $results)

Invoke the "after raw search" callback.

Parameters

mixed $results

Return Value

mixed

at line 535
protected Engine engine()

Get the engine that should handle the query.

Return Value

Engine

at line 543
string modelConnectionType()

Get the connection type for the underlying model.

Return Value

string

at line 554
static void flushState()

Flush all static state.

Return Value

void