class Illuminate / Database / Eloquent / Builder
You're browsing the documentation for an upcoming version of Laravel. The documentation and features of this release are subject to change.

Builder

Implements

Uses

Properties

protected

$query Builder

The base query builder instance.

Default: null
protected

$model Model

The model being queried.

Default: null
protected

$eagerLoad array

The relationships that should be eager loaded.

Default: []
protected static

$macros array

All of the globally registered builder macros.

Default: []
protected

$localMacros array

All of the locally registered builder macros.

Default: []
protected

$onDelete Closure

A replacement for the typical delete function.

Default: null
protected

$propertyPassthru string[]

The properties that should be returned from query builder.

Default: array
protected

$passthru string[]

The methods that should be returned from query builder.

Default: array
protected

$scopes array

Applied global scopes.

Default: []
protected

$removedScopes array

Removed global scopes.

Default: []

Methods

public

__construct ( Builder $query ) : void

Create a new Eloquent query builder instance.

Parameters

public

make ( array $attributes ) : Model |static

Create and return an un-saved model instance.

Parameters

  • $attributes array
public

withGlobalScope ( $identifier , $scope ) : $this

Register a new global scope.

Parameters

public

withoutGlobalScope ( $scope ) : $this

Remove a registered global scope.

Parameters

public

withoutGlobalScopes ( array $scopes ) : $this

Remove all or passed registered global scopes.

Parameters

  • $scopes array|null
public

removedScopes ( ) : array

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

public

whereKey ( $id ) : $this

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

Parameters

  • $id mixed
public

whereKeyNot ( $id ) : $this

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

Parameters

  • $id mixed
public

where ( $column , $operator , $value , $boolean ) : $this

Add a basic where clause to the query.

Parameters

public

firstWhere ( $column , $operator , $value , $boolean ) : Model |static|null

Add a basic where clause to the query, and return the first result.

Parameters

public

orWhere ( $column , $operator , $value ) : $this

Add an "or where" clause to the query.

Parameters

public

whereNot ( $column , $operator , $value , $boolean ) : $this

Add a basic "where not" clause to the query.

Parameters

public

orWhereNot ( $column , $operator , $value ) : $this

Add an "or where not" clause to the query.

Parameters

public

latest ( $column ) : $this

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

Parameters

public

oldest ( $column ) : $this

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

Parameters

public

hydrate ( array $items ) : Illuminate\...\Collection

Create a collection of models from plain arrays.

Parameters

  • $items array
public

fromQuery ( $query , $bindings ) : Illuminate\...\Collection

Create a collection of models from a raw query.

Parameters

  • $query string
  • $bindings array
public

find ( $id , $columns ) : Model |Illuminate\...\Collection|static[]|static|null

Find a model by its primary key.

Parameters

  • $id mixed
  • $columns array|string
public

findMany ( $ids , $columns ) : Illuminate\...\Collection

Find multiple models by their primary keys.

Parameters

  • $ids Illuminate\...\Arrayable|array
  • $columns array|string
public

findOrFail ( $id , $columns ) : Model |Illuminate\...\Collection|static|static[]

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

Parameters

  • $id mixed
  • $columns array|string
public

findOrNew ( $id , $columns ) : Model |static

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

Parameters

  • $id mixed
  • $columns array|string
public

findOr ( $id , $columns , Closure $callback ) : Model |Illuminate\...\Collection|static[]|static|mixed

Find a model by its primary key or call a callback.

Parameters

public

firstOrNew ( array $attributes , array $values ) : Model |static

Get the first record matching the attributes or instantiate it.

Parameters

  • $attributes array
  • $values array
public

firstOrCreate ( array $attributes , array $values ) : Model |static

Get the first record matching the attributes or create it.

Parameters

  • $attributes array
  • $values array
public

updateOrCreate ( array $attributes , array $values ) : Model |static

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

Parameters

  • $attributes array
  • $values array
public

firstOrFail ( $columns ) : Model |static

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

Parameters

  • $columns array|string
public

firstOr ( $columns , Closure $callback ) : Model |static|mixed

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

Parameters

public

sole ( $columns ) : Model

Execute the query and get the first result if it's the sole matching record.

Parameters

  • $columns array|string
public

value ( $column ) : mixed

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

Parameters

public

soleValue ( $column ) : mixed

Get a single column's value from the first result of a query if it's the sole matching record.

Parameters

public

valueOrFail ( $column ) : mixed

Get a single column's value from the first result of the query or throw an exception.

Parameters

public

get ( $columns ) : Illuminate\...\Collection|static[]

Execute the query as a "select" statement.

Parameters

  • $columns array|string
public

getModels ( $columns ) : Illuminate\...\Model[]|static[]

Get the hydrated models without eager loading.

Parameters

  • $columns array|string
public

eagerLoadRelations ( array $models ) : array

Eager load the relationships for the models.

Parameters

  • $models array
protected

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

Eagerly load the relationship on a set of models.

Parameters

  • $models array
  • $name string
  • $constraints Closure
public

getRelation ( $name ) : Relation

Get the relation instance for the given relation name.

Parameters

  • $name string
protected

relationsNestedUnder ( $relation ) : array

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

Parameters

  • $relation string
protected

isNestedUnder ( $relation , $name ) : bool

Determine if the relationship is nested.

Parameters

  • $relation string
  • $name string
public

cursor ( ) : Illuminate\...\LazyCollection

Get a lazy collection for the given query.

protected

enforceOrderBy ( ) : void

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

public

pluck ( $column , $key ) : Illuminate\...\Collection

Get a collection with the values of a given column.

Parameters

public

paginate ( $perPage , $columns , $pageName , $page , $total ) : LengthAwarePaginator

Paginate the given query.

Parameters

  • $perPage int|null|Closure
  • $columns array|string
  • $pageName string
  • $page int|null
  • $total Closure |int|null
public

simplePaginate ( $perPage , $columns , $pageName , $page ) : Paginator

Paginate the given query into a simple paginator.

Parameters

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

cursorPaginate ( $perPage , $columns , $cursorName , $cursor ) : CursorPaginator

Paginate the given query into a cursor paginator.

Parameters

  • $perPage int|null
  • $columns array|string
  • $cursorName string
  • $cursor Cursor |string|null
protected

ensureOrderForCursorPagination ( $shouldReverse ) : Illuminate\...\Collection

Ensure the proper order by required for cursor pagination.

Parameters

  • $shouldReverse bool
public

create ( array $attributes ) : Model |$this

Save a new model and return the instance.

Parameters

  • $attributes array
public

forceCreate ( array $attributes ) : Model |$this

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

Parameters

  • $attributes array
public

forceCreateQuietly ( array $attributes ) : Model |$this

Save a new model instance with mass assignment without raising model events.

Parameters

  • $attributes array
public

update ( array $values ) : int

Update records in the database.

Parameters

  • $values array
public

upsert ( array $values , $uniqueBy , $update ) : int

Insert new records or update the existing ones.

Parameters

  • $values array
  • $uniqueBy array|string
  • $update array|null
public

touch ( $column ) : int|false

Update the column's update timestamp.

Parameters

  • $column string|null
public

increment ( $column , $amount , array $extra ) : int

Increment a column's value by a given amount.

Parameters

  • $column string|Expression
  • $amount float|int
  • $extra array
public

decrement ( $column , $amount , array $extra ) : int

Decrement a column's value by a given amount.

Parameters

  • $column string|Expression
  • $amount float|int
  • $extra array
protected

addUpdatedAtColumn ( array $values ) : array

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

Parameters

  • $values array
protected

addUniqueIdsToUpsertValues ( array $values ) : array

Add unique IDs to the inserted values.

Parameters

  • $values array
protected

addTimestampsToUpsertValues ( array $values ) : array

Add timestamps to the inserted values.

Parameters

  • $values array
protected

addUpdatedAtToUpsertColumns ( array $update ) : array

Add the "updated at" column to the updated columns.

Parameters

  • $update array
public

delete ( ) : mixed

Delete records from the database.

public

forceDelete ( ) : mixed

Run the default delete function on the builder.

public

onDelete ( Closure $callback ) : void

Register a replacement for the default delete function.

Parameters

public

hasNamedScope ( $scope ) : bool

Determine if the given model has a scope.

Parameters

  • $scope string
public

scopes ( $scopes ) : static|mixed

Call the given local model scopes.

Parameters

  • $scopes array|string
public

applyScopes ( ) : static

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

protected

callScope ( callable $scope , array $parameters ) : mixed

Apply the given scope on the current builder instance.

Parameters

  • $scope callable
  • $parameters array
protected

callNamedScope ( $scope , array $parameters ) : mixed

Apply the given named scope on the current builder instance.

Parameters

  • $scope string
  • $parameters array
protected

addNewWheresWithinGroup ( Builder $query , $originalWhereCount ) : void

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

Parameters

  • $query Builder
  • $originalWhereCount int
protected

groupWhereSliceForScope ( Builder $query , $whereSlice ) : void

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

Parameters

protected

createNestedWhere ( $whereSlice , $boolean ) : array

Create a where array with nested where conditions.

Parameters

  • $whereSlice array
  • $boolean string
public

with ( $relations , $callback ) : $this

Set the relationships that should be eager loaded.

Parameters

  • $relations string|array
  • $callback string|Closure |null
public

without ( $relations ) : $this

Prevent the specified relations from being eager loaded.

Parameters

  • $relations mixed
public

withOnly ( $relations ) : $this

Set the relationships that should be eager loaded while removing any previously added eager loading specifications.

Parameters

  • $relations mixed
public

newModelInstance ( $attributes ) : Model |static

Create a new instance of the model being queried.

Parameters

  • $attributes array
protected

parseWithRelations ( array $relations ) : array

Parse a list of relations into individuals.

Parameters

  • $relations array
protected

prepareNestedWithRelationships ( $relations , $prefix ) : array

Prepare nested with relationships.

Parameters

  • $relations array
  • $prefix string
protected

combineConstraints ( array $constraints ) : Closure

Combine an array of constraints into a single constraint.

Parameters

  • $constraints array
protected

parseNameAndAttributeSelectionConstraint ( $name ) : array

Parse the attribute select constraints from the name.

Parameters

  • $name string
protected

createSelectWithConstraint ( $name ) : array

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

Parameters

  • $name string
protected

addNestedWiths ( $name , $results ) : array

Parse the nested relationships in a relation.

Parameters

  • $name string
  • $results array
public

withCasts ( $casts ) : $this

Apply query-time casts to the model instance.

Parameters

  • $casts array
public

getQuery ( ) : Builder

Get the underlying query builder instance.

public

setQuery ( $query ) : $this

Set the underlying query builder instance.

Parameters

public

toBase ( ) : Builder

Get a base query builder instance.

public

getEagerLoads ( ) : array

Get the relationships being eagerly loaded.

public

setEagerLoads ( array $eagerLoad ) : $this

Set the relationships being eagerly loaded.

Parameters

  • $eagerLoad array
public

withoutEagerLoad ( array $relations ) : $this

Indicate that the given relationships should not be eagerly loaded.

Parameters

  • $relations array
public

withoutEagerLoads ( ) : $this

Flush the relationships being eagerly loaded.

protected

defaultKeyName ( ) : string

Get the default key name of the table.

public

getModel ( ) : Model |static

Get the model instance being queried.

public

setModel ( Model $model ) : $this

Set a model instance for the model being queried.

Parameters

public

qualifyColumn ( $column ) : string

Qualify the given column name by the model's table.

Parameters

public

qualifyColumns ( $columns ) : array

Qualify the given columns with the model's table.

Parameters

public

getMacro ( $name ) : Closure

Get the given macro by name.

Parameters

  • $name string
public

hasMacro ( $name ) : bool

Checks if a macro is registered.

Parameters

  • $name string
public static

getGlobalMacro ( $name ) : Closure

Get the given global macro by name.

Parameters

  • $name string
public static

hasGlobalMacro ( $name ) : bool

Checks if a global macro is registered.

Parameters

  • $name string
public

__get ( $key ) : mixed

Dynamically access builder proxies.

Parameters

  • $key string
public

__call ( $method , $parameters ) : mixed

Dynamically handle calls into the query instance.

Parameters

  • $method string
  • $parameters array
public static

__callStatic ( $method , $parameters ) : mixed

Dynamically handle calls into the query instance.

Parameters

  • $method string
  • $parameters array
protected static

registerMixin ( $mixin , $replace ) : void

Register the given mixin with the builder.

Parameters

  • $mixin string
  • $replace bool
public

clone ( ) : static

Clone the Eloquent query builder.

public

__clone ( ) : void

Force a clone of the underlying query builder when cloning.