Builder
class Builder implements Builder (View source)
Traits
Properties
static protected array | $macros | The registered string macros. |
from Macroable |
ConnectionInterface | $connection | The database connection instance. |
|
Grammar | $grammar | The database query grammar instance. |
|
Processor | $processor | The database query post processor instance. |
|
array | $bindings | The current query value bindings. |
|
array | $aggregate | An aggregate function and column to be run. |
|
array | $columns | The columns that should be returned. |
|
bool|array | $distinct | Indicates if the query returns distinct results. |
|
string | $from | The table which the query is targeting. |
|
IndexHint | $indexHint | The index hint for the query. |
|
array | $joins | The table joins for the query. |
|
array | $wheres | The where constraints for the query. |
|
array | $groups | The groupings for the query. |
|
array | $havings | The having constraints for the query. |
|
array | $orders | The orderings for the query. |
|
int | $limit | The maximum number of records to return. |
|
int | $offset | The number of records to skip. |
|
array | $unions | The query union statements. |
|
int | $unionLimit | The maximum number of union records to return. |
|
int | $unionOffset | The number of union records to skip. |
|
array | $unionOrders | The orderings for the union query. |
|
string|bool | $lock | Indicates whether row locking is being used. |
|
array | $beforeQueryCallbacks | The callbacks that should be invoked before the query is executed. |
|
string[] | $operators | All of the available clause operators. |
|
string[] | $bitwiseOperators | All of the available bitwise operators. |
|
bool | $useWritePdo | Whether to use write pdo for the select. |
Methods
Apply the callback if the given "value" is (or resolves to) truthy.
Apply the callback if the given "value" is (or resolves to) falsy.
Run a map over each item while chunking.
Execute a callback over each item while chunking.
Chunk the results of a query by comparing IDs.
Execute a callback over each item while chunking by ID.
Query lazily, by chunks of the given size.
Query lazily, by chunking the results of a query by comparing IDs.
Query lazily, by chunking the results of a query by comparing IDs in descending order.
Query lazily, by chunking the results of a query by comparing IDs in a given order.
Execute the query and get the first result.
Execute the query and get the first result if it's the sole matching record.
Paginate the given query using a cursor paginator.
Get the original column name of the given column, without any aliasing.
Create a new length-aware paginator instance.
Create a new simple paginator instance.
Create a new cursor paginator instance.
Forward a method call to the given object.
Forward a method call to the given object, returning $this if the forwarded call returned itself.
Throw a bad method call exception for the given method.
Mix another object into the class.
Dynamically handle calls to the class.
Handle dynamic method calls into the method.
Create a new query builder instance.
Set the columns to be selected.
Add a new "raw" select expression to the query.
Add a raw from clause to the query.
Parse the subquery into SQL and bindings.
Prepend the database name if the given query is on another database.
Add a new select column to the query.
Force the query to only return distinct results.
Add an index hint to suggest a query index.
Add an index hint to force a query index.
Add an index hint to ignore a query index.
Add a join clause to the query.
Add a "join where" clause to the query.
Add a left join to the query.
Add a "join where" clause to the query.
Add a subquery left join to the query.
Add a right join to the query.
Add a "right join where" clause to the query.
Add a subquery right join to the query.
Add a "cross join" clause to the query.
Add a subquery cross join to the query.
Merge an array of where clauses and bindings.
Add a basic where clause to the query.
Add an array of where clauses to the query.
Prepare the value and operator for a where clause.
Determine if the given operator and value combination is legal.
Determine if the given operator is supported.
Determine if the operator is a bitwise operator.
Add an "or where" clause to the query.
Add a basic "where not" clause to the query.
Add an "or where not" clause to the query.
Add a "where" clause comparing two columns to the query.
Add an "or where" clause comparing two columns to the query.
Add a raw where clause to the query.
Add a raw or where clause to the query.
Add a "where in" clause to the query.
Add a "where not in" clause to the query.
Add an "or where not in" clause to the query.
Add a "where in raw" clause for integer values to the query.
Add an "or where in raw" clause for integer values to the query.
Add a "where not in raw" clause for integer values to the query.
Add an "or where not in raw" clause for integer values to the query.
Add a "where null" clause to the query.
Add a "where not null" clause to the query.
Add a where between statement to the query.
Add a where between statement using columns to the query.
Add an or where between statement to the query.
Add an or where between statement using columns to the query.
Add a where not between statement to the query.
Add a where not between statement using columns to the query.
Add an or where not between statement to the query.
Add an or where not between statement using columns to the query.
Add a "where date" statement to the query.
Add an "or where date" statement to the query.
Add a "where time" statement to the query.
Add an "or where time" statement to the query.
Add a "where day" statement to the query.
Add an "or where day" statement to the query.
Add a "where month" statement to the query.
Add an "or where month" statement to the query.
Add a "where year" statement to the query.
Add an "or where year" statement to the query.
Add a date based (year, month, day, time) statement to the query.
Create a new query instance for nested where condition.
Add another query builder as a nested where to the query builder.
Add a full sub-select to the query.
Add an exists clause to the query.
Add an or exists clause to the query.
Add a where not exists clause to the query.
Add an exists clause to the query.
Adds a where condition using row values.
Adds an or where condition using row values.
Add a "where JSON contains" clause to the query.
Add an "or where JSON contains" clause to the query.
Add a "where JSON not contains" clause to the query.
Add an "or where JSON not contains" clause to the query.
Add a clause that determines if a JSON path exists to the query.
Add an "or" clause that determines if a JSON path exists to the query.
Add a clause that determines if a JSON path does not exist to the query.
Add an "or" clause that determines if a JSON path does not exist to the query.
Add a "where JSON length" clause to the query.
Add an "or where JSON length" clause to the query.
Handles dynamic "where" clauses to the query.
Add a single dynamic where clause statement to the query.
Add a "where fulltext" clause to the query.
Add a "or where fulltext" clause to the query.
Add a raw groupBy clause to the query.
Add a nested having statement to the query.
Add another query builder as a nested having to the query builder.
Add a "having null" clause to the query.
Add an "or having null" clause to the query.
Add a "having not null" clause to the query.
Add an "or having not null" clause to the query.
Add a "having between " clause to the query.
Add a raw having clause to the query.
Add a raw or having clause to the query.
Add an "order by" clause to the query.
Add a descending "order by" clause to the query.
Add an "order by" clause for a timestamp to the query.
Add an "order by" clause for a timestamp to the query.
Put the query's results in random order.
Add a raw "order by" clause to the query.
Alias to set the "offset" value of the query.
Set the "offset" value of the query.
Alias to set the "limit" value of the query.
Set the "limit" value of the query.
Set the limit and offset for a given page.
Constrain the query to the previous "page" of results before a given ID.
Constrain the query to the next "page" of results after a given ID.
Remove all existing orders and optionally add a new order.
Get an array with all orders with a given column removed.
Lock the selected rows in the table.
Lock the selected rows in the table for updating.
Share lock the selected rows in the table.
Register a closure to be invoked before the query is executed.
Invoke the "before query" modification callbacks.
Get the SQL representation of the query.
Execute a query for a single record by ID.
Get a single column's value from the first result of a query.
Get a single expression value from the first result of a query.
Get a single column's value from the first result of a query if it's the sole matching record.
Execute the query as a "select" statement.
Run the query as a "select" statement against the connection.
Get a paginator only supporting simple next and previous links.
Get a paginator only supporting simple next and previous links.
Ensure the proper order by required for cursor pagination.
Get the count of the total records for the paginator.
Run a pagination count query.
Clone the existing query instance for usage in a pagination subquery.
Remove the column aliases since they will break count queries.
Get a lazy collection for the given query.
Throw an exception if the query doesn't have an orderBy clause.
Get a collection instance containing the values of a given column.
Strip off the table name or alias from a column identifier.
Retrieve column values from rows represented as objects.
Retrieve column values from rows represented as arrays.
Concatenate values of a given column as a string.
Determine if any rows exist for the current query.
Determine if no rows exist for the current query.
Execute an aggregate function on the database.
Execute a numeric aggregate function on the database.
Set the aggregate property without running the query.
Execute the given callback while selecting the given columns.
Insert new records into the database.
Insert new records into the database while ignoring errors.
Insert a new record and get the value of the primary key.
Insert new records into the table using a subquery.
Update records in the database.
Update records in a PostgreSQL database using the update from syntax.
Insert or update a record matching the attributes, and fill it with values.
Insert new records or update the existing ones.
Increment a column's value by a given amount.
Increment the given column's values by the given amounts.
Decrement a column's value by a given amount.
Decrement the given column's values by the given amounts.
Delete records from the database.
Run a truncate statement on the table.
Create a new query instance for a sub-query.
Get all of the query builder's columns in a text-only array with all expressions evaluated.
Create a raw database expression.
Get the current query value bindings in a flattened array.
Get the raw array of bindings.
Set the bindings on the query builder.
Add a binding to the query.
Cast the given binding value.
Remove all of the expressions from a list of bindings.
Get a scalar type value from an unknown type of input.
Get the default key name of the table.
Get the database connection instance.
Get the database query processor instance.
Get the query grammar instance.
Use the "write" PDO connection when executing the query.
Determine if the value is a query builder instance or a Closure.
Clone the query without the given properties.
Clone the query without the given bindings.
Dump the current SQL and bindings.
Die and dump the current SQL and bindings.
Details
$this|TWhenReturnType
when($value = null, callable $callback = null, callable $default = null)
Apply the callback if the given "value" is (or resolves to) truthy.
$this|TUnlessReturnType
unless($value = null, callable $callback = null, callable $default = null)
Apply the callback if the given "value" is (or resolves to) falsy.
bool
chunk(int $count, callable $callback)
Chunk the results of the query.
Collection
chunkMap(callable $callback, int $count = 1000)
Run a map over each item while chunking.
bool
each(callable $callback, int $count = 1000)
Execute a callback over each item while chunking.
bool
chunkById(int $count, callable $callback, string|null $column = null, string|null $alias = null)
Chunk the results of a query by comparing IDs.
bool
eachById(callable $callback, int $count = 1000, string|null $column = null, string|null $alias = null)
Execute a callback over each item while chunking by ID.
LazyCollection
lazy(int $chunkSize = 1000)
Query lazily, by chunks of the given size.
LazyCollection
lazyById(int $chunkSize = 1000, string|null $column = null, string|null $alias = null)
Query lazily, by chunking the results of a query by comparing IDs.
LazyCollection
lazyByIdDesc(int $chunkSize = 1000, string|null $column = null, string|null $alias = null)
Query lazily, by chunking the results of a query by comparing IDs in descending order.
protected LazyCollection
orderedLazyById(int $chunkSize = 1000, string|null $column = null, string|null $alias = null, bool $descending = false)
Query lazily, by chunking the results of a query by comparing IDs in a given order.
Model|object|BuildsQueries|null
first(array|string $columns = ['*'])
Execute the query and get the first result.
Model|object|BuildsQueries|null
sole(array|string $columns = ['*'])
Execute the query and get the first result if it's the sole matching record.
protected CursorPaginator
paginateUsingCursor(int $perPage, array|string $columns = ['*'], string $cursorName = 'cursor', Cursor|string|null $cursor = null)
Paginate the given query using a cursor paginator.
protected string
getOriginalColumnNameForCursorPagination(Builder|Builder $builder, string $parameter)
Get the original column name of the given column, without any aliasing.
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.
protected CursorPaginator
cursorPaginator(Collection $items, int $perPage, Cursor $cursor, array $options)
Create a new cursor paginator instance.
$this
tap(callable $callback)
Pass the query to a given callback.
Collection
explain()
Explains the query.
protected mixed
forwardCallTo(mixed $object, string $method, array $parameters)
Forward a method call to the given object.
protected mixed
forwardDecoratedCallTo(mixed $object, string $method, array $parameters)
Forward a method call to the given object, returning $this if the forwarded call returned itself.
static protected void
throwBadMethodCallException(string $method)
Throw a bad method call exception for the given method.
static void
macro(string $name, object|callable $macro)
Register a custom macro.
static void
mixin(object $mixin, bool $replace = true)
Mix another object into the class.
static bool
hasMacro(string $name)
Checks 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)
Handle dynamic method calls into the method.
void
__construct(ConnectionInterface $connection, Grammar $grammar = null, Processor $processor = null)
Create a new query builder instance.
$this
select(array|mixed $columns = ['*'])
Set the columns to be selected.
$this
selectSub(Closure|Builder|Builder|string $query, string $as)
Add a subselect expression to the query.
$this
selectRaw(string $expression, array $bindings = [])
Add a new "raw" select expression to the query.
$this
fromSub(Closure|Builder|Builder|string $query, string $as)
Makes "from" fetch from a subquery.
$this
fromRaw(string $expression, mixed $bindings = [])
Add a raw from clause to the query.
protected array
parseSub(mixed $query)
Parse the subquery into SQL and bindings.
protected mixed
prependDatabaseNameIfCrossDatabaseQuery(mixed $query)
Prepend the database name if the given query is on another database.
$this
addSelect(array|mixed $column)
Add a new select column to the query.
$this
distinct()
Force the query to only return distinct results.
$this
from(Closure|Builder|Builder|string $table, string|null $as = null)
Set the table which the query is targeting.
$this
useIndex(string $index)
Add an index hint to suggest a query index.
$this
forceIndex(string $index)
Add an index hint to force a query index.
$this
ignoreIndex(string $index)
Add an index hint to ignore a query index.
$this
join(Expression|string $table, Closure|string $first, string|null $operator = null, string|null $second = null, string $type = 'inner', bool $where = false)
Add a join clause to the query.
$this
joinWhere(Expression|string $table, Closure|string $first, string $operator, string $second, string $type = 'inner')
Add a "join where" clause to the query.
$this
joinSub(Closure|Builder|Builder|string $query, string $as, Closure|string $first, string|null $operator = null, string|null $second = null, string $type = 'inner', bool $where = false)
Add a subquery join clause to the query.
$this
leftJoin(Expression|string $table, Closure|string $first, string|null $operator = null, string|null $second = null)
Add a left join to the query.
$this
leftJoinWhere(Expression|string $table, Closure|string $first, string $operator, string $second)
Add a "join where" clause to the query.
$this
leftJoinSub(Closure|Builder|Builder|string $query, string $as, Closure|string $first, string|null $operator = null, string|null $second = null)
Add a subquery left join to the query.
$this
rightJoin(Expression|string $table, Closure|string $first, string|null $operator = null, string|null $second = null)
Add a right join to the query.
$this
rightJoinWhere(Expression|string $table, Closure|string $first, string $operator, string $second)
Add a "right join where" clause to the query.
$this
rightJoinSub(Closure|Builder|Builder|string $query, string $as, Closure|string $first, string|null $operator = null, string|null $second = null)
Add a subquery right join to the query.
$this
crossJoin(Expression|string $table, Closure|string|null $first = null, string|null $operator = null, string|null $second = null)
Add a "cross join" clause to the query.
$this
crossJoinSub(Closure|Builder|Builder|string $query, string $as)
Add a subquery cross join to the query.
protected JoinClause
newJoinClause(Builder $parentQuery, string $type, string $table)
Get a new join clause.
$this
mergeWheres(array $wheres, array $bindings)
Merge an array of where clauses and bindings.
$this
where(Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null, string $boolean = 'and')
Add a basic where clause to the query.
protected $this
addArrayOfWheres(array $column, string $boolean, string $method = 'where')
Add an array of where clauses to the query.
array
prepareValueAndOperator(string $value, string $operator, bool $useDefault = false)
Prepare the value and operator for a where clause.
protected bool
invalidOperatorAndValue(string $operator, mixed $value)
Determine if the given operator and value combination is legal.
Prevents using Null values with invalid operators.
protected bool
invalidOperator(string $operator)
Determine if the given operator is supported.
protected bool
isBitwiseOperator(string $operator)
Determine if the operator is a bitwise operator.
$this
orWhere(Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)
Add an "or where" clause to the query.
$this
whereNot(Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null, string $boolean = 'and')
Add a basic "where not" clause to the query.
$this
orWhereNot(Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)
Add an "or where not" clause to the query.
$this
whereColumn(string|array $first, string|null $operator = null, string|null $second = null, string|null $boolean = 'and')
Add a "where" clause comparing two columns to the query.
$this
orWhereColumn(string|array $first, string|null $operator = null, string|null $second = null)
Add an "or where" clause comparing two columns to the query.
$this
whereRaw(string $sql, mixed $bindings = [], string $boolean = 'and')
Add a raw where clause to the query.
$this
orWhereRaw(string $sql, mixed $bindings = [])
Add a raw or where clause to the query.
$this
whereIn(Expression|string $column, mixed $values, string $boolean = 'and', bool $not = false)
Add a "where in" clause to the query.
$this
orWhereIn(Expression|string $column, mixed $values)
Add an "or where in" clause to the query.
$this
whereNotIn(Expression|string $column, mixed $values, string $boolean = 'and')
Add a "where not in" clause to the query.
$this
orWhereNotIn(Expression|string $column, mixed $values)
Add an "or where not in" clause to the query.
$this
whereIntegerInRaw(string $column, Arrayable|array $values, string $boolean = 'and', bool $not = false)
Add a "where in raw" clause for integer values to the query.
$this
orWhereIntegerInRaw(string $column, Arrayable|array $values)
Add an "or where in raw" clause for integer values to the query.
$this
whereIntegerNotInRaw(string $column, Arrayable|array $values, string $boolean = 'and')
Add a "where not in raw" clause for integer values to the query.
$this
orWhereIntegerNotInRaw(string $column, Arrayable|array $values)
Add an "or where not in raw" clause for integer values to the query.
$this
whereNull(string|array|Expression $columns, string $boolean = 'and', bool $not = false)
Add a "where null" clause to the query.
$this
orWhereNull(string|array|Expression $column)
Add an "or where null" clause to the query.
$this
whereNotNull(string|array|Expression $columns, string $boolean = 'and')
Add a "where not null" clause to the query.
$this
whereBetween(Expression|string $column, iterable $values, string $boolean = 'and', bool $not = false)
Add a where between statement to the query.
$this
whereBetweenColumns(Expression|string $column, array $values, string $boolean = 'and', bool $not = false)
Add a where between statement using columns to the query.
$this
orWhereBetween(Expression|string $column, iterable $values)
Add an or where between statement to the query.
$this
orWhereBetweenColumns(Expression|string $column, array $values)
Add an or where between statement using columns to the query.
$this
whereNotBetween(Expression|string $column, iterable $values, string $boolean = 'and')
Add a where not between statement to the query.
$this
whereNotBetweenColumns(Expression|string $column, array $values, string $boolean = 'and')
Add a where not between statement using columns to the query.
$this
orWhereNotBetween(Expression|string $column, iterable $values)
Add an or where not between statement to the query.
$this
orWhereNotBetweenColumns(Expression|string $column, array $values)
Add an or where not between statement using columns to the query.
$this
orWhereNotNull(Expression|string $column)
Add an "or where not null" clause to the query.
$this
whereDate(Expression|string $column, string $operator, DateTimeInterface|string|null $value = null, string $boolean = 'and')
Add a "where date" statement to the query.
$this
orWhereDate(Expression|string $column, string $operator, DateTimeInterface|string|null $value = null)
Add an "or where date" statement to the query.
$this
whereTime(Expression|string $column, string $operator, DateTimeInterface|string|null $value = null, string $boolean = 'and')
Add a "where time" statement to the query.
$this
orWhereTime(Expression|string $column, string $operator, DateTimeInterface|string|null $value = null)
Add an "or where time" statement to the query.
$this
whereDay(Expression|string $column, string $operator, DateTimeInterface|string|int|null $value = null, string $boolean = 'and')
Add a "where day" statement to the query.
$this
orWhereDay(Expression|string $column, string $operator, DateTimeInterface|string|int|null $value = null)
Add an "or where day" statement to the query.
$this
whereMonth(Expression|string $column, string $operator, DateTimeInterface|string|int|null $value = null, string $boolean = 'and')
Add a "where month" statement to the query.
$this
orWhereMonth(Expression|string $column, string $operator, DateTimeInterface|string|int|null $value = null)
Add an "or where month" statement to the query.
$this
whereYear(Expression|string $column, string $operator, DateTimeInterface|string|int|null $value = null, string $boolean = 'and')
Add a "where year" statement to the query.
$this
orWhereYear(Expression|string $column, string $operator, DateTimeInterface|string|int|null $value = null)
Add an "or where year" statement to the query.
protected $this
addDateBasedWhere(string $type, Expression|string $column, string $operator, mixed $value, string $boolean = 'and')
Add a date based (year, month, day, time) statement to the query.
$this
whereNested(Closure $callback, string $boolean = 'and')
Add a nested where statement to the query.
Builder
forNestedWhere()
Create a new query instance for nested where condition.
$this
addNestedWhereQuery(Builder $query, string $boolean = 'and')
Add another query builder as a nested where to the query builder.
protected $this
whereSub(Expression|string $column, string $operator, Closure $callback, string $boolean)
Add a full sub-select to the query.
$this
whereExists(Closure|Builder|Builder $callback, string $boolean = 'and', bool $not = false)
Add an exists clause to the query.
$this
orWhereExists(Closure|Builder|Builder $callback, bool $not = false)
Add an or exists clause to the query.
$this
whereNotExists(Closure|Builder|Builder $callback, string $boolean = 'and')
Add a where not exists clause to the query.
$this
orWhereNotExists(Closure|Builder|Builder $callback)
Add a where not exists clause to the query.
$this
addWhereExistsQuery(Builder $query, string $boolean = 'and', bool $not = false)
Add an exists clause to the query.
$this
whereRowValues(array $columns, string $operator, array $values, string $boolean = 'and')
Adds a where condition using row values.
$this
orWhereRowValues(array $columns, string $operator, array $values)
Adds an or where condition using row values.
$this
whereJsonContains(string $column, mixed $value, string $boolean = 'and', bool $not = false)
Add a "where JSON contains" clause to the query.
$this
orWhereJsonContains(string $column, mixed $value)
Add an "or where JSON contains" clause to the query.
$this
whereJsonDoesntContain(string $column, mixed $value, string $boolean = 'and')
Add a "where JSON not contains" clause to the query.
$this
orWhereJsonDoesntContain(string $column, mixed $value)
Add an "or where JSON not contains" clause to the query.
$this
whereJsonContainsKey(string $column, string $boolean = 'and', bool $not = false)
Add a clause that determines if a JSON path exists to the query.
$this
orWhereJsonContainsKey(string $column)
Add an "or" clause that determines if a JSON path exists to the query.
$this
whereJsonDoesntContainKey(string $column, string $boolean = 'and')
Add a clause that determines if a JSON path does not exist to the query.
$this
orWhereJsonDoesntContainKey(string $column)
Add an "or" clause that determines if a JSON path does not exist to the query.
$this
whereJsonLength(string $column, mixed $operator, mixed $value = null, string $boolean = 'and')
Add a "where JSON length" clause to the query.
$this
orWhereJsonLength(string $column, mixed $operator, mixed $value = null)
Add an "or where JSON length" clause to the query.
$this
dynamicWhere(string $method, array $parameters)
Handles dynamic "where" clauses to the query.
protected void
addDynamic(string $segment, string $connector, array $parameters, int $index)
Add a single dynamic where clause statement to the query.
$this
whereFullText(string|string[] $columns, string $value, array $options = [], string $boolean = 'and')
Add a "where fulltext" clause to the query.
$this
orWhereFullText(string|string[] $columns, string $value, array $options = [])
Add a "or where fulltext" clause to the query.
$this
groupBy(array|Expression|string ...$groups)
Add a "group by" clause to the query.
$this
groupByRaw(string $sql, array $bindings = [])
Add a raw groupBy clause to the query.
$this
having(Closure|string $column, string|int|float|null $operator = null, string|int|float|null $value = null, string $boolean = 'and')
Add a "having" clause to the query.
$this
orHaving(Closure|string $column, string|int|float|null $operator = null, string|int|float|null $value = null)
Add an "or having" clause to the query.
$this
havingNested(Closure $callback, string $boolean = 'and')
Add a nested having statement to the query.
$this
addNestedHavingQuery(Builder $query, string $boolean = 'and')
Add another query builder as a nested having to the query builder.
$this
havingNull(string|array $columns, string $boolean = 'and', bool $not = false)
Add a "having null" clause to the query.
$this
orHavingNull(string $column)
Add an "or having null" clause to the query.
$this
havingNotNull(string|array $columns, string $boolean = 'and')
Add a "having not null" clause to the query.
$this
orHavingNotNull(string $column)
Add an "or having not null" clause to the query.
$this
havingBetween(string $column, iterable $values, string $boolean = 'and', bool $not = false)
Add a "having between " clause to the query.
$this
havingRaw(string $sql, array $bindings = [], string $boolean = 'and')
Add a raw having clause to the query.
$this
orHavingRaw(string $sql, array $bindings = [])
Add a raw or having clause to the query.
$this
orderBy(Closure|Builder|Builder|Expression|string $column, string $direction = 'asc')
Add an "order by" clause to the query.
$this
orderByDesc(Closure|Builder|Builder|Expression|string $column)
Add a descending "order by" clause to the query.
$this
latest(Closure|Builder|Expression|string $column = 'created_at')
Add an "order by" clause for a timestamp to the query.
$this
oldest(Closure|Builder|Expression|string $column = 'created_at')
Add an "order by" clause for a timestamp to the query.
$this
inRandomOrder(string|int $seed = '')
Put the query's results in random order.
$this
orderByRaw(string $sql, array $bindings = [])
Add a raw "order by" clause to the query.
$this
skip(int $value)
Alias to set the "offset" value of the query.
$this
offset(int $value)
Set the "offset" value of the query.
$this
take(int $value)
Alias to set the "limit" value of the query.
$this
limit(int $value)
Set the "limit" value of the query.
$this
forPage(int $page, int $perPage = 15)
Set the limit and offset for a given page.
$this
forPageBeforeId(int $perPage = 15, int|null $lastId = 0, string $column = 'id')
Constrain the query to the previous "page" of results before a given ID.
$this
forPageAfterId(int $perPage = 15, int|null $lastId = 0, string $column = 'id')
Constrain the query to the next "page" of results after a given ID.
$this
reorder(Closure|Builder|Expression|string|null $column = null, string $direction = 'asc')
Remove all existing orders and optionally add a new order.
protected array
removeExistingOrdersFor(string $column)
Get an array with all orders with a given column removed.
$this
lock(string|bool $value = true)
Lock the selected rows in the table.
$this
lockForUpdate()
Lock the selected rows in the table for updating.
$this
sharedLock()
Share lock the selected rows in the table.
$this
beforeQuery(callable $callback)
Register a closure to be invoked before the query is executed.
void
applyBeforeQueryCallbacks()
Invoke the "before query" modification callbacks.
string
toSql()
Get the SQL representation of the query.
mixed|Builder
find(int|string $id, array|string $columns = ['*'])
Execute a query for a single record by ID.
mixed|Builder
findOr(mixed $id, Closure|array|string $columns = ['*'], Closure $callback = null)
Execute a query for a single record by ID or call a callback.
mixed
value(string $column)
Get a single column's value from the first result of a query.
mixed
rawValue(string $expression, array $bindings = [])
Get a single expression value from the first result of a query.
mixed
soleValue(string $column)
Get a single column's value from the first result of a query if it's the sole matching record.
Collection
get(array|string $columns = ['*'])
Execute the query as a "select" statement.
protected array
runSelect()
Run the query as a "select" statement against the connection.
LengthAwarePaginator
paginate(int|Closure $perPage = 15, array|string $columns = ['*'], string $pageName = 'page', int|null $page = null)
Paginate the given query into a simple paginator.
Paginator
simplePaginate(int $perPage = 15, array|string $columns = ['*'], string $pageName = 'page', int|null $page = null)
Get a paginator only supporting simple next and previous links.
This is more efficient on larger data-sets, etc.
CursorPaginator
cursorPaginate(int|null $perPage = 15, array|string $columns = ['*'], string $cursorName = 'cursor', Cursor|string|null $cursor = null)
Get a paginator only supporting simple next and previous links.
This is more efficient on larger data-sets, etc.
protected Collection
ensureOrderForCursorPagination(bool $shouldReverse = false)
Ensure the proper order by required for cursor pagination.
int
getCountForPagination(array $columns = ['*'])
Get the count of the total records for the paginator.
protected array
runPaginationCountQuery(array $columns = ['*'])
Run a pagination count query.
protected Builder
cloneForPaginationCount()
Clone the existing query instance for usage in a pagination subquery.
protected array
withoutSelectAliases(array $columns)
Remove the column aliases since they will break count queries.
LazyCollection
cursor()
Get a lazy collection for the given query.
protected void
enforceOrderBy()
Throw an exception if the query doesn't have an orderBy clause.
Collection
pluck(Expression|string $column, string|null $key = null)
Get a collection instance containing the values of a given column.
protected string|null
stripTableForPluck(string $column)
Strip off the table name or alias from a column identifier.
protected Collection
pluckFromObjectColumn(array $queryResult, string $column, string $key)
Retrieve column values from rows represented as objects.
protected Collection
pluckFromArrayColumn(array $queryResult, string $column, string $key)
Retrieve column values from rows represented as arrays.
string
implode(string $column, string $glue = '')
Concatenate values of a given column as a string.
bool
exists()
Determine if any rows exist for the current query.
bool
doesntExist()
Determine if no rows exist for the current query.
mixed
existsOr(Closure $callback)
Execute the given callback if no rows exist for the current query.
mixed
doesntExistOr(Closure $callback)
Execute the given callback if rows exist for the current query.
int
count(Expression|string $columns = '*')
Retrieve the "count" result of the query.
mixed
min(Expression|string $column)
Retrieve the minimum value of a given column.
mixed
max(Expression|string $column)
Retrieve the maximum value of a given column.
mixed
sum(Expression|string $column)
Retrieve the sum of the values of a given column.
mixed
avg(Expression|string $column)
Retrieve the average of the values of a given column.
mixed
average(Expression|string $column)
Alias for the "avg" method.
mixed
aggregate(string $function, array $columns = ['*'])
Execute an aggregate function on the database.
float|int
numericAggregate(string $function, array $columns = ['*'])
Execute a numeric aggregate function on the database.
protected $this
setAggregate(string $function, array $columns)
Set the aggregate property without running the query.
protected mixed
onceWithColumns(array $columns, callable $callback)
Execute the given callback while selecting the given columns.
After running the callback, the columns are reset to the original value.
bool
insert(array $values)
Insert new records into the database.
int
insertOrIgnore(array $values)
Insert new records into the database while ignoring errors.
int
insertGetId(array $values, string|null $sequence = null)
Insert a new record and get the value of the primary key.
int
insertUsing(array $columns, Closure|Builder|Builder|string $query)
Insert new records into the table using a subquery.
int
update(array $values)
Update records in the database.
int
updateFrom(array $values)
Update records in a PostgreSQL database using the update from syntax.
bool
updateOrInsert(array $attributes, array $values = [])
Insert or update a record matching the attributes, and fill it with values.
int
upsert(array $values, array|string $uniqueBy, array|null $update = null)
Insert new records or update the existing ones.
int
increment(string $column, float|int $amount = 1, array $extra = [])
Increment a column's value by a given amount.
int
incrementEach(array $columns, array $extra = [])
Increment the given column's values by the given amounts.
int
decrement(string $column, float|int $amount = 1, array $extra = [])
Decrement a column's value by a given amount.
int
decrementEach(array $columns, array $extra = [])
Decrement the given column's values by the given amounts.
int
delete(mixed $id = null)
Delete records from the database.
void
truncate()
Run a truncate statement on the table.
Builder
newQuery()
Get a new instance of the query builder.
protected Builder
forSubQuery()
Create a new query instance for a sub-query.
array
getColumns()
Get all of the query builder's columns in a text-only array with all expressions evaluated.
Expression
raw(mixed $value)
Create a raw database expression.
array
getBindings()
Get the current query value bindings in a flattened array.
array
getRawBindings()
Get the raw array of bindings.
$this
setBindings(array $bindings, string $type = 'where')
Set the bindings on the query builder.
$this
addBinding(mixed $value, string $type = 'where')
Add a binding to the query.
mixed
castBinding(mixed $value)
Cast the given binding value.
$this
mergeBindings(Builder $query)
Merge an array of bindings into our bindings.
array
cleanBindings(array $bindings)
Remove all of the expressions from a list of bindings.
protected mixed
flattenValue(mixed $value)
Get a scalar type value from an unknown type of input.
protected string
defaultKeyName()
Get the default key name of the table.
ConnectionInterface
getConnection()
Get the database connection instance.
Processor
getProcessor()
Get the database query processor instance.
Grammar
getGrammar()
Get the query grammar instance.
$this
useWritePdo()
Use the "write" PDO connection when executing the query.
protected bool
isQueryable(mixed $value)
Determine if the value is a query builder instance or a Closure.
Builder
clone()
Clone the query.
Builder
cloneWithout(array $properties)
Clone the query without the given properties.
Builder
cloneWithoutBindings(array $except)
Clone the query without the given bindings.
$this
dump()
Dump the current SQL and bindings.
never
dd()
Die and dump the current SQL and bindings.