class Builder mixin Builder

Traits

Properties

protected Builder $query

The base query builder instance.

protected Model $model

The model being queried.

protected array $eagerLoad

The relationships that should be eager loaded.

static protected array $macros

All of the globally registered builder macros.

protected array $localMacros

All of the locally registered builder macros.

protected Closure $onDelete

A replacement for the typical delete function.

protected array $passthru

The methods that should be returned from query builder.

protected array $scopes

Applied global scopes.

protected array $removedScopes

Removed global scopes.

Methods

bool
chunk(int $count, callable $callback)

Chunk the results of the query.

bool
each(callable $callback, int $count = 1000)

Execute a callback over each item while chunking.

first(array $columns = ['*'])

Execute the query and get the first result.

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

Apply the callback's query changes if the given "value" is true.

tap(Closure $callback)

Pass the query to a given callback.

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

Apply the callback's query changes if the given "value" is false.

paginator(Collection $items, int $total, int $perPage, int $currentPage, array $options)

Create a new length-aware paginator instance.

simplePaginator(Collection $items, int $perPage, int $currentPage, array $options)

Create a new simple paginator instance.

has(string $relation, string $operator = '>=', int $count = 1, string $boolean = 'and', Closure $callback = null)

Add a relationship count / exists condition to the query.

hasNested(string $relations, string $operator = '>=', int $count = 1, string $boolean = 'and', Closure|null $callback = null)

Add nested relationship count / exists conditions to the query.

orHas(string $relation, string $operator = '>=', int $count = 1)

Add a relationship count / exists condition to the query with an "or".

doesntHave(string $relation, string $boolean = 'and', Closure $callback = null)

Add a relationship count / exists condition to the query.

orDoesntHave(string $relation)

Add a relationship count / exists condition to the query with an "or".

whereHas(string $relation, Closure $callback = null, string $operator = '>=', int $count = 1)

Add a relationship count / exists condition to the query with where clauses.

orWhereHas(string $relation, Closure $callback = null, string $operator = '>=', int $count = 1)

Add a relationship count / exists condition to the query with where clauses and an "or".

whereDoesntHave(string $relation, Closure $callback = null)

Add a relationship count / exists condition to the query with where clauses.

orWhereDoesntHave(string $relation, Closure $callback = null)

Add a relationship count / exists condition to the query with where clauses and an "or".

$this
withCount(mixed $relations)

Add subselect queries to count the relations.

addHasWhere(Builder $hasQuery, Relation $relation, string $operator, int $count, string $boolean)

Add the "has" condition where clause to the query.

mergeConstraintsFrom(Builder $from)

Merge the where constraints from another query to the current query.

$this
addWhereCountQuery(Builder $query, string $operator = '>=', int $count = 1, string $boolean = 'and')

Add a sub-query count clause to this query.

getRelationWithoutConstraints(string $relation)

Get the "has relation" base query instance.

bool
canUseExistsForExistenceCheck(string $operator, int $count)

Check if we can run an "exists" query to optimize performance.

void
__construct(Builder $query)

Create a new Eloquent query builder instance.

make(array $attributes = [])

Create and return an un-saved model instance.

$this
withGlobalScope(string $identifier, Scope|Closure $scope)

Register a new global scope.

$this
withoutGlobalScope(Scope|string $scope)

Remove a registered global scope.

$this
withoutGlobalScopes(array $scopes = null)

Remove all or passed registered global scopes.

array
removedScopes()

Get an array of global scopes that were removed from the query.

$this
whereKey(mixed $id)

Add a where clause on the primary key to the query.

$this
where(string|array|Closure $column, string $operator = null, mixed $value = null, string $boolean = 'and')

Add a basic where clause to the query.

orWhere(string|array|Closure $column, string $operator = null, mixed $value = null)

Add an "or where" clause to the query.

hydrate(array $items)

Create a collection of models from plain arrays.

fromQuery(string $query, array $bindings = [])

Create a collection of models from a raw query.

find(mixed $id, array $columns = ['*'])

Find a model by its primary key.

findMany(array $ids, array $columns = ['*'])

Find multiple models by their primary keys.

findOrFail(mixed $id, array $columns = ['*'])

Find a model by its primary key or throw an exception.

findOrNew(mixed $id, array $columns = ['*'])

Find a model by its primary key or return fresh model instance.

firstOrNew(array $attributes, array $values = [])

Get the first record matching the attributes or instantiate it.

firstOrCreate(array $attributes, array $values = [])

Get the first record matching the attributes or create it.

updateOrCreate(array $attributes, array $values = [])

Create or update a record matching the attributes, and fill it with values.

firstOrFail(array $columns = ['*'])

Execute the query and get the first result or throw an exception.

Model|Builder|mixed
firstOr(Closure|array $columns = ['*'], Closure $callback = null)

Execute the query and get the first result or call a callback.

mixed
value(string $column)

Get a single column's value from the first result of a query.

get(array $columns = ['*'])

Execute the query as a "select" statement.

Model[]
getModels(array $columns = ['*'])

Get the hydrated models without eager loading.

array
eagerLoadRelations(array $models)

Eager load the relationships for the models.

array
eagerLoadRelation(array $models, string $name, Closure $constraints)

Eagerly load the relationship on a set of models.

getRelation(string $name)

Get the relation instance for the given relation name.

array
relationsNestedUnder(string $relation)

Get the deeply nested relations for a given top-level relation.

bool
isNestedUnder(string $relation, string $name)

Determine if the relationship is nested.

cursor()

Get a generator for the given query.

bool
chunkById(int $count, callable $callback, string $column = null, string|null $alias = null)

Chunk the results of a query by comparing numeric IDs.

void
enforceOrderBy()

Add a generic "order by" clause if the query doesn't already have one.

pluck(string $column, string|null $key = null)

Get an array with the values of a given column.

paginate(int $perPage = null, array $columns = ['*'], string $pageName = 'page', int|null $page = null)

Paginate the given query.

simplePaginate(int $perPage = null, array $columns = ['*'], string $pageName = 'page', int|null $page = null)

Paginate the given query into a simple paginator.

Model|$this
create(array $attributes = [])

Save a new model and return the instance.

Model|$this
forceCreate(array $attributes)

Save a new model and return the instance. Allow mass-assignment.

int
update(array $values)

Update a record in the database.

int
increment(string $column, int $amount = 1, array $extra = [])

Increment a column's value by a given amount.

int
decrement(string $column, int $amount = 1, array $extra = [])

Decrement a column's value by a given amount.

array
addUpdatedAtColumn(array $values)

Add the "updated at" column to an array of values.

mixed
delete()

Delete a record from the database.

mixed
forceDelete()

Run the default delete function on the builder.

void
onDelete(Closure $callback)

Register a replacement for the default delete function.

mixed
scopes(array $scopes)

Call the given local model scopes.

applyScopes()

Apply the scopes to the Eloquent builder instance and return it.

mixed
callScope(callable $scope, array $parameters = [])

Apply the given scope on the current builder instance.

void
addNewWheresWithinGroup(Builder $query, int $originalWhereCount)

Nest where conditions by slicing them at the given where count.

void
groupWhereSliceForScope(Builder $query, array $whereSlice)

Slice where conditions at the given offset and add them to the query as a nested condition.

array
createNestedWhere(array $whereSlice, string $boolean = 'and')

Create a where array with nested where conditions.

$this
with(mixed $relations)

Set the relationships that should be eager loaded.

$this
without(mixed $relations)

Prevent the specified relations from being eager loaded.

newModelInstance(array $attributes = [])

Create a new instance of the model being queried.

array
parseWithRelations(array $relations)

Parse a list of relations into individuals.

array
createSelectWithConstraint(string $name)

Create a constraint to select the given columns for the relation.

array
addNestedWiths(string $name, array $results)

Parse the nested relationships in a relation.

getQuery()

Get the underlying query builder instance.

$this
setQuery(Builder $query)

Set the underlying query builder instance.

toBase()

Get a base query builder instance.

array
getEagerLoads()

Get the relationships being eagerly loaded.

$this
setEagerLoads(array $eagerLoad)

Set the relationships being eagerly loaded.

getModel()

Get the model instance being queried.

$this
setModel(Model $model)

Set a model instance for the model being queried.

getMacro(string $name)

Get the given macro by name.

mixed
__call(string $method, array $parameters)

Dynamically handle calls into the query instance.

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

Dynamically handle calls into the query instance.

void
__clone()

Force a clone of the underlying query builder when cloning.

Details

in BuildsQueries at line 18
bool chunk(int $count, callable $callback)

Chunk the results of the query.

Parameters

int $count
callable $callback

Return Value

bool

in BuildsQueries at line 56
bool each(callable $callback, int $count = 1000)

Execute a callback over each item while chunking.

Parameters

callable $callback
int $count

Return Value

bool

in BuildsQueries at line 73
Model|BuildsQueries|null first(array $columns = ['*'])

Execute the query and get the first result.

Parameters

array $columns

Return Value

Model|BuildsQueries|null

in BuildsQueries at line 86
mixed when(mixed $value, callable $callback, callable $default = null)

Apply the callback's query changes if the given "value" is true.

Parameters

mixed $value
callable $callback
callable $default

Return Value

mixed

in BuildsQueries at line 103
Builder tap(Closure $callback)

Pass the query to a given callback.

Parameters

Closure $callback

Return Value

Builder

in BuildsQueries at line 116
mixed unless(mixed $value, callable $callback, callable $default = null)

Apply the callback's query changes if the given "value" is false.

Parameters

mixed $value
callable $callback
callable $default

Return Value

mixed

in BuildsQueries at line 137
protected LengthAwarePaginator paginator(Collection $items, int $total, int $perPage, int $currentPage, array $options)

Create a new length-aware paginator instance.

Parameters

Collection $items
int $total
int $perPage
int $currentPage
array $options

Return Value

LengthAwarePaginator

in BuildsQueries at line 153
protected Paginator simplePaginator(Collection $items, int $perPage, int $currentPage, array $options)

Create a new simple paginator instance.

Parameters

Collection $items
int $perPage
int $currentPage
array $options

Return Value

Paginator

Builder|QueriesRelationships has(string $relation, string $operator = '>=', int $count = 1, string $boolean = 'and', Closure $callback = null)

Add a relationship count / exists condition to the query.

Parameters

string $relation
string $operator
int $count
string $boolean
Closure $callback

Return Value

Builder|QueriesRelationships

protected Builder|QueriesRelationships hasNested(string $relations, string $operator = '>=', int $count = 1, string $boolean = 'and', Closure|null $callback = null)

Add nested relationship count / exists conditions to the query.

Sets up recursive call to whereHas until we finish the nested relation.

Parameters

string $relations
string $operator
int $count
string $boolean
Closure|null $callback

Return Value

Builder|QueriesRelationships

Builder|QueriesRelationships orHas(string $relation, string $operator = '>=', int $count = 1)

Add a relationship count / exists condition to the query with an "or".

Parameters

string $relation
string $operator
int $count

Return Value

Builder|QueriesRelationships

in QueriesRelationships at line 105
Builder|QueriesRelationships doesntHave(string $relation, string $boolean = 'and', Closure $callback = null)

Add a relationship count / exists condition to the query.

Parameters

string $relation
string $boolean
Closure $callback

Return Value

Builder|QueriesRelationships

in QueriesRelationships at line 116
Builder|QueriesRelationships orDoesntHave(string $relation)

Add a relationship count / exists condition to the query with an "or".

Parameters

string $relation

Return Value

Builder|QueriesRelationships

in QueriesRelationships at line 130
Builder|QueriesRelationships whereHas(string $relation, Closure $callback = null, string $operator = '>=', int $count = 1)

Add a relationship count / exists condition to the query with where clauses.

Parameters

string $relation
Closure $callback
string $operator
int $count

Return Value

Builder|QueriesRelationships

in QueriesRelationships at line 144
Builder|QueriesRelationships orWhereHas(string $relation, Closure $callback = null, string $operator = '>=', int $count = 1)

Add a relationship count / exists condition to the query with where clauses and an "or".

Parameters

string $relation
Closure $callback
string $operator
int $count

Return Value

Builder|QueriesRelationships

in QueriesRelationships at line 156
Builder|QueriesRelationships whereDoesntHave(string $relation, Closure $callback = null)

Add a relationship count / exists condition to the query with where clauses.

Parameters

string $relation
Closure $callback

Return Value

Builder|QueriesRelationships

in QueriesRelationships at line 168
Builder|QueriesRelationships orWhereDoesntHave(string $relation, Closure $callback = null)

Add a relationship count / exists condition to the query with where clauses and an "or".

Parameters

string $relation
Closure $callback

Return Value

Builder|QueriesRelationships

in QueriesRelationships at line 179
$this withCount(mixed $relations)

Add subselect queries to count the relations.

Parameters

mixed $relations

Return Value

$this

in QueriesRelationships at line 237
protected Builder|QueriesRelationships addHasWhere(Builder $hasQuery, Relation $relation, string $operator, int $count, string $boolean)

Add the "has" condition where clause to the query.

Parameters

Builder $hasQuery
Relation $relation
string $operator
int $count
string $boolean

Return Value

Builder|QueriesRelationships

in QueriesRelationships at line 252
Builder|QueriesRelationships mergeConstraintsFrom(Builder $from)

Merge the where constraints from another query to the current query.

Parameters

Builder $from

Return Value

Builder|QueriesRelationships

in QueriesRelationships at line 277
protected $this addWhereCountQuery(Builder $query, string $operator = '>=', int $count = 1, string $boolean = 'and')

Add a sub-query count clause to this query.

Parameters

Builder $query
string $operator
int $count
string $boolean

Return Value

$this

in QueriesRelationships at line 295
protected Relation getRelationWithoutConstraints(string $relation)

Get the "has relation" base query instance.

Parameters

string $relation

Return Value

Relation

in QueriesRelationships at line 309
protected bool canUseExistsForExistenceCheck(string $operator, int $count)

Check if we can run an "exists" query to optimize performance.

Parameters

string $operator
int $count

Return Value

bool

at line 94
void __construct(Builder $query)

Create a new Eloquent query builder instance.

Parameters

Builder $query

Return Value

void

at line 105
Model make(array $attributes = [])

Create and return an un-saved model instance.

Parameters

array $attributes

Return Value

Model

at line 117
$this withGlobalScope(string $identifier, Scope|Closure $scope)

Register a new global scope.

Parameters

string $identifier
Scope|Closure $scope

Return Value

$this

at line 134
$this withoutGlobalScope(Scope|string $scope)

Remove a registered global scope.

Parameters

Scope|string $scope

Return Value

$this

at line 153
$this withoutGlobalScopes(array $scopes = null)

Remove all or passed registered global scopes.

Parameters

array $scopes

Return Value

$this

at line 171
array removedScopes()

Get an array of global scopes that were removed from the query.

Return Value

array

at line 182
$this whereKey(mixed $id)

Add a where clause on the primary key to the query.

Parameters

mixed $id

Return Value

$this

at line 202
$this where(string|array|Closure $column, string $operator = null, mixed $value = null, string $boolean = 'and')

Add a basic where clause to the query.

Parameters

string|array|Closure $column
string $operator
mixed $value
string $boolean

Return Value

$this

at line 225
Builder|Builder orWhere(string|array|Closure $column, string $operator = null, mixed $value = null)

Add an "or where" clause to the query.

Parameters

string|array|Closure $column
string $operator
mixed $value

Return Value

Builder|Builder

at line 236
Collection hydrate(array $items)

Create a collection of models from plain arrays.

Parameters

array $items

Return Value

Collection

at line 252
Collection fromQuery(string $query, array $bindings = [])

Create a collection of models from a raw query.

Parameters

string $query
array $bindings

Return Value

Collection

at line 266
Model|Collection|Builder[]|Builder|null find(mixed $id, array $columns = ['*'])

Find a model by its primary key.

Parameters

mixed $id
array $columns

Return Value

Model|Collection|Builder[]|Builder|null

at line 282
Collection findMany(array $ids, array $columns = ['*'])

Find multiple models by their primary keys.

Parameters

array $ids
array $columns

Return Value

Collection

at line 300
Model|Collection findOrFail(mixed $id, array $columns = ['*'])

Find a model by its primary key or throw an exception.

Parameters

mixed $id
array $columns

Return Value

Model|Collection

Exceptions

ModelNotFoundException

at line 324
Model findOrNew(mixed $id, array $columns = ['*'])

Find a model by its primary key or return fresh model instance.

Parameters

mixed $id
array $columns

Return Value

Model

at line 340
Model firstOrNew(array $attributes, array $values = [])

Get the first record matching the attributes or instantiate it.

Parameters

array $attributes
array $values

Return Value

Model

at line 356
Model firstOrCreate(array $attributes, array $values = [])

Get the first record matching the attributes or create it.

Parameters

array $attributes
array $values

Return Value

Model

at line 374
Model updateOrCreate(array $attributes, array $values = [])

Create or update a record matching the attributes, and fill it with values.

Parameters

array $attributes
array $values

Return Value

Model

at line 389
Model|Builder firstOrFail(array $columns = ['*'])

Execute the query and get the first result or throw an exception.

Parameters

array $columns

Return Value

Model|Builder

Exceptions

ModelNotFoundException

at line 405
Model|Builder|mixed firstOr(Closure|array $columns = ['*'], Closure $callback = null)

Execute the query and get the first result or call a callback.

Parameters

Closure|array $columns
Closure $callback

Return Value

Model|Builder|mixed

at line 426
mixed value(string $column)

Get a single column's value from the first result of a query.

Parameters

string $column

Return Value

mixed

at line 439
Collection|Builder[] get(array $columns = ['*'])

Execute the query as a "select" statement.

Parameters

array $columns

Return Value

Collection|Builder[]

at line 459
Model[] getModels(array $columns = ['*'])

Get the hydrated models without eager loading.

Parameters

array $columns

Return Value

Model[]

at line 472
array eagerLoadRelations(array $models)

Eager load the relationships for the models.

Parameters

array $models

Return Value

array

at line 494
protected array eagerLoadRelation(array $models, string $name, Closure $constraints)

Eagerly load the relationship on a set of models.

Parameters

array $models
string $name
Closure $constraints

Return Value

array

at line 520
Relation getRelation(string $name)

Get the relation instance for the given relation name.

Parameters

string $name

Return Value

Relation

at line 551
protected array relationsNestedUnder(string $relation)

Get the deeply nested relations for a given top-level relation.

Parameters

string $relation

Return Value

array

at line 574
protected bool isNestedUnder(string $relation, string $name)

Determine if the relationship is nested.

Parameters

string $relation
string $name

Return Value

bool

at line 584
Generator cursor()

Get a generator for the given query.

Return Value

Generator

at line 600
bool chunkById(int $count, callable $callback, string $column = null, string|null $alias = null)

Chunk the results of a query by comparing numeric IDs.

Parameters

int $count
callable $callback
string $column
string|null $alias

Return Value

bool

at line 640
protected void enforceOrderBy()

Add a generic "order by" clause if the query doesn't already have one.

Return Value

void

at line 654
Collection pluck(string $column, string|null $key = null)

Get an array with the values of a given column.

Parameters

string $column
string|null $key

Return Value

Collection

at line 683
LengthAwarePaginator paginate(int $perPage = null, array $columns = ['*'], string $pageName = 'page', int|null $page = null)

Paginate the given query.

Parameters

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

Return Value

LengthAwarePaginator

Exceptions

InvalidArgumentException

at line 708
Paginator simplePaginate(int $perPage = null, array $columns = ['*'], string $pageName = 'page', int|null $page = null)

Paginate the given query into a simple paginator.

Parameters

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

Return Value

Paginator

at line 731
Model|$this create(array $attributes = [])

Save a new model and return the instance.

Parameters

array $attributes

Return Value

Model|$this

at line 744
Model|$this forceCreate(array $attributes)

Save a new model and return the instance. Allow mass-assignment.

Parameters

array $attributes

Return Value

Model|$this

at line 757
int update(array $values)

Update a record in the database.

Parameters

array $values

Return Value

int

at line 770
int increment(string $column, int $amount = 1, array $extra = [])

Increment a column's value by a given amount.

Parameters

string $column
int $amount
array $extra

Return Value

int

at line 785
int decrement(string $column, int $amount = 1, array $extra = [])

Decrement a column's value by a given amount.

Parameters

string $column
int $amount
array $extra

Return Value

int

at line 798
protected array addUpdatedAtColumn(array $values)

Add the "updated at" column to an array of values.

Parameters

array $values

Return Value

array

at line 815
mixed delete()

Delete a record from the database.

Return Value

mixed

at line 831
mixed forceDelete()

Run the default delete function on the builder.

Since we do not apply scopes here, the row will actually be deleted.

Return Value

mixed

at line 842
void onDelete(Closure $callback)

Register a replacement for the default delete function.

Parameters

Closure $callback

Return Value

void

at line 853
mixed scopes(array $scopes)

Call the given local model scopes.

Parameters

array $scopes

Return Value

mixed

at line 882
Builder|Builder applyScopes()

Apply the scopes to the Eloquent builder instance and return it.

Return Value

Builder|Builder

at line 922
protected mixed callScope(callable $scope, array $parameters = [])

Apply the given scope on the current builder instance.

Parameters

callable $scope
array $parameters

Return Value

mixed

at line 950
protected void addNewWheresWithinGroup(Builder $query, int $originalWhereCount)

Nest where conditions by slicing them at the given where count.

Parameters

Builder $query
int $originalWhereCount

Return Value

void

at line 975
protected void groupWhereSliceForScope(Builder $query, array $whereSlice)

Slice where conditions at the given offset and add them to the query as a nested condition.

Parameters

Builder $query
array $whereSlice

Return Value

void

at line 998
protected array createNestedWhere(array $whereSlice, string $boolean = 'and')

Create a where array with nested where conditions.

Parameters

array $whereSlice
string $boolean

Return Value

array

at line 1013
$this with(mixed $relations)

Set the relationships that should be eager loaded.

Parameters

mixed $relations

Return Value

$this

at line 1028
$this without(mixed $relations)

Prevent the specified relations from being eager loaded.

Parameters

mixed $relations

Return Value

$this

at line 1043
Model newModelInstance(array $attributes = [])

Create a new instance of the model being queried.

Parameters

array $attributes

Return Value

Model

at line 1056
protected array parseWithRelations(array $relations)

Parse a list of relations into individuals.

Parameters

array $relations

Return Value

array

at line 1091
protected array createSelectWithConstraint(string $name)

Create a constraint to select the given columns for the relation.

Parameters

string $name

Return Value

array

at line 1105
protected array addNestedWiths(string $name, array $results)

Parse the nested relationships in a relation.

Parameters

string $name
array $results

Return Value

array

at line 1130
Builder getQuery()

Get the underlying query builder instance.

Return Value

Builder

at line 1141
$this setQuery(Builder $query)

Set the underlying query builder instance.

Parameters

Builder $query

Return Value

$this

at line 1153
Builder toBase()

Get a base query builder instance.

Return Value

Builder

at line 1163
array getEagerLoads()

Get the relationships being eagerly loaded.

Return Value

array

at line 1174
$this setEagerLoads(array $eagerLoad)

Set the relationships being eagerly loaded.

Parameters

array $eagerLoad

Return Value

$this

at line 1186
Model getModel()

Get the model instance being queried.

Return Value

Model

at line 1197
$this setModel(Model $model)

Set a model instance for the model being queried.

Parameters

Model $model

Return Value

$this

at line 1212
Closure getMacro(string $name)

Get the given macro by name.

Parameters

string $name

Return Value

Closure

at line 1224
mixed __call(string $method, array $parameters)

Dynamically handle calls into the query instance.

Parameters

string $method
array $parameters

Return Value

mixed

at line 1268
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls into the query instance.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

at line 1292
void __clone()

Force a clone of the underlying query builder when cloning.

Return Value

void