Builder
class Builder (View source)
Traits
BuildsQueries |
QueriesRelationships |
ForwardsCalls |
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. | from BuildsQueries |
bool | each(callable $callback, int $count = 1000) Execute a callback over each item while chunking. | from BuildsQueries |
Model|object|BuildsQueries|null | first(array $columns = ['*']) Execute the query and get the first result. | from BuildsQueries |
mixed|$this | when(mixed $value, callable $callback, callable $default = null) Apply the callback's query changes if the given "value" is true. | from BuildsQueries |
Builder | tap(Closure $callback) Pass the query to a given callback. | from BuildsQueries |
mixed|$this | unless(mixed $value, callable $callback, callable $default = null) Apply the callback's query changes if the given "value" is false. | from BuildsQueries |
LengthAwarePaginator | paginator(Collection $items, int $total, int $perPage, int $currentPage, array $options) Create a new length-aware paginator instance. | from BuildsQueries |
Paginator | simplePaginator(Collection $items, int $perPage, int $currentPage, array $options) Create a new simple paginator instance. | from BuildsQueries |
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. | from QueriesRelationships |
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. | from QueriesRelationships |
Builder|QueriesRelationships | orHas(string $relation, string $operator = '>=', int $count = 1) Add a relationship count / exists condition to the query with an "or". | from QueriesRelationships |
Builder|QueriesRelationships | doesntHave(string $relation, string $boolean = 'and', Closure $callback = null) Add a relationship count / exists condition to the query. | from QueriesRelationships |
Builder|QueriesRelationships | orDoesntHave(string $relation) Add a relationship count / exists condition to the query with an "or". | from QueriesRelationships |
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. | from QueriesRelationships |
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". | from QueriesRelationships |
Builder|QueriesRelationships | whereDoesntHave(string $relation, Closure $callback = null) Add a relationship count / exists condition to the query with where clauses. | from QueriesRelationships |
Builder|QueriesRelationships | orWhereDoesntHave(string $relation, Closure $callback = null) Add a relationship count / exists condition to the query with where clauses and an "or". | from QueriesRelationships |
$this | withCount(mixed $relations) Add subselect queries to count the relations. | from QueriesRelationships |
Builder|QueriesRelationships | addHasWhere(Builder $hasQuery, Relation $relation, string $operator, int $count, string $boolean) Add the "has" condition where clause to the query. | from QueriesRelationships |
Builder|QueriesRelationships | mergeConstraintsFrom(Builder $from) Merge the where constraints from another query to the current query. | from QueriesRelationships |
$this | addWhereCountQuery(Builder $query, string $operator = '>=', int $count = 1, string $boolean = 'and') Add a sub-query count clause to this query. | from QueriesRelationships |
Relation | getRelationWithoutConstraints(string $relation) Get the "has relation" base query instance. | from QueriesRelationships |
bool | canUseExistsForExistenceCheck(string $operator, int $count) Check if we can run an "exists" query to optimize performance. | from QueriesRelationships |
mixed | forwardCallTo(mixed $object, string $method, array $parameters) Forward a method call to the given object. | from ForwardsCalls |
static void | throwBadMethodCallException(string $method) Throw a bad method call exception for the given method. | from ForwardsCalls |
void | __construct(Builder $query) Create a new Eloquent query builder instance. | |
Model | 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 | whereKeyNot(mixed $id) Add a where clause on the primary key to the query. | |
$this | where(string|array|Closure $column, mixed $operator = null, mixed $value = null, string $boolean = 'and') Add a basic where clause to the query. | |
Builder|Builder | orWhere(Closure|array|string $column, mixed $operator = null, mixed $value = null) Add an "or where" clause to the query. | |
$this | latest(string $column = null) Add an "order by" clause for a timestamp to the query. | |
$this | oldest(string $column = null) Add an "order by" clause for a timestamp to the query. | |
Collection | hydrate(array $items) Create a collection of models from plain arrays. | |
Collection | fromQuery(string $query, array $bindings = []) Create a collection of models from a raw query. | |
Model|Collection|Builder[]|Builder|null | find(mixed $id, array $columns = ['*']) Find a model by its primary key. | |
Collection | findMany(Arrayable|array $ids, array $columns = ['*']) Find multiple models by their primary keys. | |
Model|Collection|Builder|Builder[] | findOrFail(mixed $id, array $columns = ['*']) Find a model by its primary key or throw an exception. | |
Model|Builder | findOrNew(mixed $id, array $columns = ['*']) Find a model by its primary key or return fresh model instance. | |
Model|Builder | firstOrNew(array $attributes, array $values = []) Get the first record matching the attributes or instantiate it. | |
Model|Builder | firstOrCreate(array $attributes, array $values = []) Get the first record matching the attributes or create it. | |
Model|Builder | updateOrCreate(array $attributes, array $values = []) Create or update a record matching the attributes, and fill it with values. | |
Model|Builder | 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. | |
Collection|Builder[] | get(array $columns = ['*']) Execute the query as a "select" statement. | |
Model[]|Builder[] | 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. | |
Relation | 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. | |
Generator | cursor() Get a generator for the given query. | |
bool | chunkById(int $count, callable $callback, string|null $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. | |
Collection | pluck(string $column, string|null $key = null) Get an array with the values of a given column. | |
LengthAwarePaginator | paginate(int $perPage = null, array $columns = ['*'], string $pageName = 'page', int|null $page = null) Paginate the given query. | |
Paginator | 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, float|int $amount = 1, array $extra = []) Increment a column's value by a given amount. | |
int | decrement(string $column, float|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. | |
Builder|Builder | 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. | |
Model|Builder | 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. | |
Builder | getQuery() Get the underlying query builder instance. | |
$this | setQuery(Builder $query) Set the underlying query builder instance. | |
Builder | 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. | |
Model|Builder | getModel() Get the model instance being queried. | |
$this | setModel(Model $model) Set a model instance for the model being queried. | |
string | qualifyColumn(string $column) Qualify the given column name by the model's table. | |
Closure | 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
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.
Model|object|BuildsQueries|null first(array $columns = ['*'])
Execute the query and get the first result.
mixed|$this when(mixed $value, callable $callback, callable $default = null)
Apply the callback's query changes if the given "value" is true.
mixed|$this unless(mixed $value, callable $callback, callable $default = null)
Apply the callback's query changes if the given "value" is false.
protected LengthAwarePaginator paginator(Collection $items, int $total, int $perPage, int $currentPage, array $options)
Create a new length-aware paginator instance.
protected Paginator simplePaginator(Collection $items, int $perPage, int $currentPage, array $options)
Create a new simple paginator instance.
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.
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.
Builder|QueriesRelationships orHas(string $relation, string $operator = '>=', int $count = 1)
Add a relationship count / exists condition to the query with an "or".
Builder|QueriesRelationships doesntHave(string $relation, string $boolean = 'and', Closure $callback = null)
Add a relationship count / exists condition to the query.
Builder|QueriesRelationships orDoesntHave(string $relation)
Add a relationship count / exists condition to the query with an "or".
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.
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".
Builder|QueriesRelationships whereDoesntHave(string $relation, Closure $callback = null)
Add a relationship count / exists condition to the query with where clauses.
Builder|QueriesRelationships 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.
protected Builder|QueriesRelationships addHasWhere(Builder $hasQuery, Relation $relation, string $operator, int $count, string $boolean)
Add the "has" condition where clause to the query.
Builder|QueriesRelationships mergeConstraintsFrom(Builder $from)
Merge the where constraints from another query to the current query.
protected $this addWhereCountQuery(Builder $query, string $operator = '>=', int $count = 1, string $boolean = 'and')
Add a sub-query count clause to this query.
protected Relation getRelationWithoutConstraints(string $relation)
Get the "has relation" base query instance.
protected bool canUseExistsForExistenceCheck(string $operator, int $count)
Check if we can run an "exists" query to optimize performance.
protected mixed forwardCallTo(mixed $object, string $method, array $parameters)
Forward a method call to the given object.
static protected void throwBadMethodCallException(string $method)
Throw a bad method call exception for the given method.
void __construct(Builder $query)
Create a new Eloquent query builder instance.
Model make(array $attributes = [])
Create and return an un-saved model instance.
$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 whereKeyNot(mixed $id)
Add a where clause on the primary key to the query.
$this where(string|array|Closure $column, mixed $operator = null, mixed $value = null, string $boolean = 'and')
Add a basic where clause to the query.
Builder|Builder orWhere(Closure|array|string $column, mixed $operator = null, mixed $value = null)
Add an "or where" clause to the query.
$this latest(string $column = null)
Add an "order by" clause for a timestamp to the query.
$this oldest(string $column = null)
Add an "order by" clause for a timestamp to the query.
Collection hydrate(array $items)
Create a collection of models from plain arrays.
Collection fromQuery(string $query, array $bindings = [])
Create a collection of models from a raw query.
Model|Collection|Builder[]|Builder|null find(mixed $id, array $columns = ['*'])
Find a model by its primary key.
Collection findMany(Arrayable|array $ids, array $columns = ['*'])
Find multiple models by their primary keys.
Model|Collection|Builder|Builder[] findOrFail(mixed $id, array $columns = ['*'])
Find a model by its primary key or throw an exception.
Model|Builder findOrNew(mixed $id, array $columns = ['*'])
Find a model by its primary key or return fresh model instance.
Model|Builder firstOrNew(array $attributes, array $values = [])
Get the first record matching the attributes or instantiate it.
Model|Builder firstOrCreate(array $attributes, array $values = [])
Get the first record matching the attributes or create it.
Model|Builder updateOrCreate(array $attributes, array $values = [])
Create or update a record matching the attributes, and fill it with values.
Model|Builder 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.
Collection|Builder[] get(array $columns = ['*'])
Execute the query as a "select" statement.
array eagerLoadRelations(array $models)
Eager load the relationships for the models.
protected array eagerLoadRelation(array $models, string $name, Closure $constraints)
Eagerly load the relationship on a set of models.
Relation getRelation(string $name)
Get the relation instance for the given relation name.
protected array relationsNestedUnder(string $relation)
Get the deeply nested relations for a given top-level relation.
protected bool isNestedUnder(string $relation, string $name)
Determine if the relationship is nested.
Generator cursor()
Get a generator for the given query.
bool chunkById(int $count, callable $callback, string|null $column = null, string|null $alias = null)
Chunk the results of a query by comparing numeric IDs.
protected void enforceOrderBy()
Add a generic "order by" clause if the query doesn't already have one.
Collection pluck(string $column, string|null $key = null)
Get an array with the values of a given column.
LengthAwarePaginator paginate(int $perPage = null, array $columns = ['*'], string $pageName = 'page', int|null $page = null)
Paginate the given query.
Paginator 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, float|int $amount = 1, array $extra = [])
Increment a column's value by a given amount.
int decrement(string $column, float|int $amount = 1, array $extra = [])
Decrement a column's value by a given amount.
protected 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.
Since we do not apply scopes here, the row will actually be deleted.
void onDelete(Closure $callback)
Register a replacement for the default delete function.
mixed scopes(array $scopes)
Call the given local model scopes.
protected mixed callScope(callable $scope, array $parameters = [])
Apply the given scope on the current builder instance.
protected void addNewWheresWithinGroup(Builder $query, int $originalWhereCount)
Nest where conditions by slicing them at the given where count.
protected void groupWhereSliceForScope(Builder $query, array $whereSlice)
Slice where conditions at the given offset and add them to the query as a nested condition.
protected 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.
Model|Builder newModelInstance(array $attributes = [])
Create a new instance of the model being queried.
protected array parseWithRelations(array $relations)
Parse a list of relations into individuals.
protected array createSelectWithConstraint(string $name)
Create a constraint to select the given columns for the relation.
protected array addNestedWiths(string $name, array $results)
Parse the nested relationships in a relation.
Builder getQuery()
Get the underlying query builder instance.
$this setQuery(Builder $query)
Set the underlying query builder instance.
Builder 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.
$this setModel(Model $model)
Set a model instance for the model being queried.
string qualifyColumn(string $column)
Qualify the given column name by the model's table.
Closure 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.
© Taylor Otwell
Licensed under the MIT License.
Laravel is a trademark of Taylor Otwell.
https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Builder.html