W3cubDocs

/Laravel 5.2

Builder

class Builder (View source)

Methods

void __construct( Builder $query)

Create a new Eloquent query builder 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.

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

Find a model by its primary key.

Collection findMany( array $ids, array $columns = array('*'))

Find multiple models by their primary keys.

Model|Collection findOrFail( mixed $id, array $columns = array('*'))

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

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

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

Model firstOrNew( array $attributes)

Get the first record matching the attributes or instantiate it.

Model firstOrCreate( array $attributes)

Get the first record matching the attributes or create it.

Model updateOrCreate( array $attributes, array $values = array())

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

Model|Builder|null first( array $columns = array('*'))

Execute the query and get the first result.

Model|Builder firstOrFail( array $columns = array('*'))

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

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

Execute the query as a "select" statement.

mixed value( string $column)

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

bool chunk( int $count, callable $callback)

Chunk the results of the query.

bool chunkById( int $count, callable $callback, string $column = 'id')

Chunk the results of a query by comparing numeric IDs.

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

Execute a callback over each item while chunking.

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

Get an array with the values of a given column.

Collection lists( string $column, string $key = null)

Alias for the "pluck" method.

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

Paginate the given query.

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

Paginate the given query into a simple paginator.

int update( array $values)

Update a record in the database.

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

Increment a column's value by a given amount.

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

Decrement a column's value by a given amount.

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.

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

Get the hydrated models without eager loading.

array eagerLoadRelations( array $models)

Eager load the relationships for the models.

Relation getRelation( string $name)

Get the relation instance for the given relation name.

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

Add a basic where clause to the query.

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

Add an "or where" clause to the query.

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

Add a relationship count / exists condition to the query.

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

Add a relationship count / exists condition to the query.

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

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

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

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

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

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

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

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

$this with( mixed $relations)

Set the relationships that should be eager loaded.

Builder|Builder applyScopes()

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

Builder|Builder getQuery()

Get the underlying query builder instance.

Builder toBase()

Get a base query builder instance.

$this setQuery( Builder $query)

Set the underlying query builder instance.

array getEagerLoads()

Get the relationships being eagerly loaded.

$this setEagerLoads( array $eagerLoad)

Set the relationships being eagerly loaded.

Model getModel()

Get the model instance being queried.

$this setModel( Model $model)

Set a model instance for the model being queried.

void macro( string $name, Closure $callback)

Extend the builder with a given callback.

Closure getMacro( string $name)

Get the given macro by name.

mixed __call( string $method, array $parameters)

Dynamically handle calls into the query instance.

void __clone()

Force a clone of the underlying query builder when cloning.

Details

void __construct( Builder $query)

Create a new Eloquent query builder instance.

Parameters

Builder $query

Return Value

void

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

Register a new global scope.

Parameters

string $identifier
Scope|Closure $scope

Return Value

$this

$this withoutGlobalScope( Scope|string $scope)

Remove a registered global scope.

Parameters

Scope|string $scope

Return Value

$this

$this withoutGlobalScopes( array $scopes = null)

Remove all or passed registered global scopes.

Parameters

array $scopes

Return Value

$this

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

Find a model by its primary key.

Parameters

mixed $id
array $columns

Return Value

Model|Collection|Builder[]|Builder|null

Collection findMany( array $ids, array $columns = array('*'))

Find multiple models by their primary keys.

Parameters

array $ids
array $columns

Return Value

Collection

Model|Collection findOrFail( mixed $id, array $columns = array('*'))

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

Parameters

mixed $id
array $columns

Return Value

Model|Collection

Exceptions

ModelNotFoundException

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

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

Parameters

mixed $id
array $columns

Return Value

Model

Model firstOrNew( array $attributes)

Get the first record matching the attributes or instantiate it.

Parameters

array $attributes

Return Value

Model

Model firstOrCreate( array $attributes)

Get the first record matching the attributes or create it.

Parameters

array $attributes

Return Value

Model

Model updateOrCreate( array $attributes, array $values = array())

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

Parameters

array $attributes
array $values

Return Value

Model

Model|Builder|null first( array $columns = array('*'))

Execute the query and get the first result.

Parameters

array $columns

Return Value

Model|Builder|null

Model|Builder firstOrFail( array $columns = array('*'))

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

Parameters

array $columns

Return Value

Model|Builder

Exceptions

ModelNotFoundException

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

Execute the query as a "select" statement.

Parameters

array $columns

Return Value

Collection|Builder[]

mixed value( string $column)

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

Parameters

string $column

Return Value

mixed

bool chunk( int $count, callable $callback)

Chunk the results of the query.

Parameters

int $count
callable $callback

Return Value

bool

bool chunkById( int $count, callable $callback, string $column = 'id')

Chunk the results of a query by comparing numeric IDs.

Parameters

int $count
callable $callback
string $column

Return Value

bool

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

Execute a callback over each item while chunking.

Parameters

callable $callback
int $count

Return Value

bool

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

Collection lists( string $column, string $key = null)

Alias for the "pluck" method.

Parameters

string $column
string $key

Return Value

Collection

LengthAwarePaginator paginate( int $perPage = null, array $columns = array('*'), 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

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

Paginate the given query into a simple paginator.

Parameters

int $perPage
array $columns
string $pageName

Return Value

Paginator

int update( array $values)

Update a record in the database.

Parameters

array $values

Return Value

int

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

Increment a column's value by a given amount.

Parameters

string $column
int $amount
array $extra

Return Value

int

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

Decrement a column's value by a given amount.

Parameters

string $column
int $amount
array $extra

Return Value

int

mixed delete()

Delete a record from the database.

Return Value

mixed

mixed forceDelete()

Run the default delete function on the builder.

Return Value

mixed

void onDelete( Closure $callback)

Register a replacement for the default delete function.

Parameters

Closure $callback

Return Value

void

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

Get the hydrated models without eager loading.

Parameters

array $columns

Return Value

Model[]

array eagerLoadRelations( array $models)

Eager load the relationships for the models.

Parameters

array $models

Return Value

array

Relation getRelation( string $name)

Get the relation instance for the given relation name.

Parameters

string $name

Return Value

Relation

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

Add a basic where clause to the query.

Parameters

string $column
string $operator
mixed $value
string $boolean

Return Value

$this

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

Add an "or where" clause to the query.

Parameters

string $column
string $operator
mixed $value

Return Value

Builder|Builder

Builder|Builder 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|Builder

Builder|Builder 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|Builder

Builder|Builder whereHas( string $relation, Closure $callback, 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|Builder

Builder|Builder 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|Builder

Builder|Builder 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|Builder

Builder|Builder orWhereHas( string $relation, Closure $callback, 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|Builder

$this with( mixed $relations)

Set the relationships that should be eager loaded.

Parameters

mixed $relations

Return Value

$this

Builder|Builder applyScopes()

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

Return Value

Builder|Builder

Builder|Builder getQuery()

Get the underlying query builder instance.

Return Value

Builder|Builder

Builder toBase()

Get a base query builder instance.

Return Value

Builder

$this setQuery( Builder $query)

Set the underlying query builder instance.

Parameters

Builder $query

Return Value

$this

array getEagerLoads()

Get the relationships being eagerly loaded.

Return Value

array

$this setEagerLoads( array $eagerLoad)

Set the relationships being eagerly loaded.

Parameters

array $eagerLoad

Return Value

$this

Model getModel()

Get the model instance being queried.

Return Value

Model

$this setModel( Model $model)

Set a model instance for the model being queried.

Parameters

Model $model

Return Value

$this

void macro( string $name, Closure $callback)

Extend the builder with a given callback.

Parameters

string $name
Closure $callback

Return Value

void

Closure getMacro( string $name)

Get the given macro by name.

Parameters

string $name

Return Value

Closure

mixed __call( string $method, array $parameters)

Dynamically handle calls into the query instance.

Parameters

string $method
array $parameters

Return Value

mixed

void __clone()

Force a clone of the underlying query builder when cloning.

Return Value

void

© Taylor Otwell
Licensed under the MIT License.
Laravel is a trademark of Taylor Otwell.
https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Builder.html