trait QueriesRelationships mixin Builder

Methods

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

Add a relationship count / exists condition to the query.

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

Add nested relationship count / exists conditions to the query.

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

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

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

Add a relationship count / exists condition to the query.

orDoesntHave(Relation|string $relation)

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

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

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

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

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

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

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

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

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

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

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

hasMorph(MorphTo|string $relation, string|array $types, string $operator = '>=', Expression|int $count = 1, string $boolean = 'and', Closure|null $callback = null)

Add a polymorphic relationship count / exists condition to the query.

getBelongsToRelation(MorphTo $relation, string $type)

Get the BelongsTo relationship for a single polymorphic type.

orHasMorph(MorphTo|string $relation, string|array $types, string $operator = '>=', Expression|int $count = 1)

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

doesntHaveMorph(MorphTo|string $relation, string|array $types, string $boolean = 'and', Closure|null $callback = null)

Add a polymorphic relationship count / exists condition to the query.

orDoesntHaveMorph(MorphTo|string $relation, string|array $types)

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

whereHasMorph(MorphTo|string $relation, string|array $types, Closure|null $callback = null, string $operator = '>=', Expression|int $count = 1)

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

orWhereHasMorph(MorphTo|string $relation, string|array $types, Closure|null $callback = null, string $operator = '>=', Expression|int $count = 1)

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

whereDoesntHaveMorph(MorphTo|string $relation, string|array $types, Closure|null $callback = null)

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

orWhereDoesntHaveMorph(MorphTo|string $relation, string|array $types, Closure|null $callback = null)

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

whereRelation(Relation|string $relation, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a basic where clause to a relationship query.

withWhereRelation(Relation|string $relation, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a basic where clause to a relationship query and eager-load the relationship with the same conditions.

orWhereRelation(Relation|string $relation, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add an "or where" clause to a relationship query.

whereDoesntHaveRelation(Relation|string $relation, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a basic count / exists condition to a relationship query.

orWhereDoesntHaveRelation(Relation|string $relation, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add an "or where" clause to a relationship query.

whereMorphRelation(MorphTo|string $relation, string|array $types, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a polymorphic relationship condition to the query with a where clause.

orWhereMorphRelation(MorphTo|string $relation, string|array $types, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a polymorphic relationship condition to the query with an "or where" clause.

whereMorphDoesntHaveRelation(MorphTo|string $relation, string|array $types, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a polymorphic relationship condition to the query with a doesn't have clause.

orWhereMorphDoesntHaveRelation(MorphTo|string $relation, string|array $types, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a polymorphic relationship condition to the query with an "or doesn't have" clause.

whereMorphedTo(MorphTo|string $relation, mixed $model, string $boolean = 'and')

Add a morph-to relationship condition to the query.

whereNotMorphedTo(MorphTo|string $relation, mixed $model, string $boolean = 'and')

Add a not morph-to relationship condition to the query.

orWhereMorphedTo(MorphTo|string $relation, mixed $model)

Add a morph-to relationship condition to the query with an "or where" clause.

orWhereNotMorphedTo(MorphTo|string $relation, mixed $model)

Add a not morph-to relationship condition to the query with an "or where" clause.

whereBelongsTo(mixed $related, string|null $relationshipName = null, string $boolean = 'and')

Add a "belongs to" relationship where clause to the query.

orWhereBelongsTo(mixed $related, string|null $relationshipName = null)

Add a "BelongsTo" relationship with an "or where" clause to the query.

whereAttachedTo(mixed $related, string|null $relationshipName = null, string $boolean = 'and')

Add a "belongs to many" relationship where clause to the query.

orWhereAttachedTo(mixed $related, string|null $relationshipName = null)

Add a "belongs to many" relationship with an "or where" clause to the query.

withAggregate(mixed $relations, Expression|string $column, string|null $function = null)

Add subselect queries to include an aggregate value for a relationship.

string
getRelationHashedColumn(string $column, Relation $relation)

Get the relation hashed column name for the given column and relation.

withCount(mixed $relations)

Add subselect queries to count the relations.

withMax(string|array $relation, Expression|string $column)

Add subselect queries to include the max of the relation's column.

withMin(string|array $relation, Expression|string $column)

Add subselect queries to include the min of the relation's column.

withSum(string|array $relation, Expression|string $column)

Add subselect queries to include the sum of the relation's column.

withAvg(string|array $relation, Expression|string $column)

Add subselect queries to include the average of the relation's column.

withExists(string|array $relation)

Add subselect queries to include the existence of related models.

addHasWhere(Builder $hasQuery, Relation $relation, string $operator, Expression|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.

array
requalifyWhereTables(array $wheres, string $from, string $to)

Updates the table name for any columns with a new qualified name.

addWhereCountQuery(Builder $query, string $operator = '>=', Expression|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, Expression|int $count)

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

Details

at line 39
QueriesRelationships has(Relation|string $relation, string $operator = '>=', Expression|int $count = 1, string $boolean = 'and', Closure|null $callback = null)

Add a relationship count / exists condition to the query.

Parameters

Relation|string $relation
string $operator
Expression|int $count
string $boolean
Closure|null $callback

Return Value

QueriesRelationships

Exceptions

RuntimeException

at line 90
protected QueriesRelationships hasNested(string $relations, string $operator = '>=', Expression|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
Expression|int $count
string $boolean
Closure|null $callback

Return Value

QueriesRelationships

at line 127
QueriesRelationships orHas(Relation|string $relation, string $operator = '>=', Expression|int $count = 1)

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

Parameters

Relation|string $relation
string $operator
Expression|int $count

Return Value

QueriesRelationships

at line 140
QueriesRelationships doesntHave(Relation|string $relation, string $boolean = 'and', Closure|null $callback = null)

Add a relationship count / exists condition to the query.

Parameters

Relation|string $relation
string $boolean
Closure|null $callback

Return Value

QueriesRelationships

at line 150
QueriesRelationships orDoesntHave(Relation|string $relation)

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

Parameters

Relation|string $relation

Return Value

QueriesRelationships

at line 163
QueriesRelationships whereHas(Relation|string $relation, Closure|null $callback = null, string $operator = '>=', Expression|int $count = 1)

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

Parameters

Relation|string $relation
Closure|null $callback
string $operator
Expression|int $count

Return Value

QueriesRelationships

at line 175
QueriesRelationships withWhereHas(string $relation, Closure|null $callback = null, string $operator = '>=', Expression|int $count = 1)

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

Also load the relationship with the same condition.

Parameters

string $relation
Closure|null $callback
string $operator
Expression|int $count

Return Value

QueriesRelationships

at line 189
QueriesRelationships orWhereHas(Relation|string $relation, Closure|null $callback = null, string $operator = '>=', Expression|int $count = 1)

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

Parameters

Relation|string $relation
Closure|null $callback
string $operator
Expression|int $count

Return Value

QueriesRelationships

at line 202
QueriesRelationships whereDoesntHave(Relation|string $relation, Closure|null $callback = null)

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

Parameters

Relation|string $relation
Closure|null $callback

Return Value

QueriesRelationships

at line 215
QueriesRelationships orWhereDoesntHave(Relation|string $relation, Closure|null $callback = null)

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

Parameters

Relation|string $relation
Closure|null $callback

Return Value

QueriesRelationships

at line 229
QueriesRelationships hasMorph(MorphTo|string $relation, string|array $types, string $operator = '>=', Expression|int $count = 1, string $boolean = 'and', Closure|null $callback = null)

Add a polymorphic relationship count / exists condition to the query.

Parameters

MorphTo|string $relation
string|array $types
string $operator
Expression|int $count
string $boolean
Closure|null $callback

Return Value

QueriesRelationships

at line 290
protected BelongsTo getBelongsToRelation(MorphTo $relation, string $type)

Get the BelongsTo relationship for a single polymorphic type.

Parameters

MorphTo $relation
string $type

Return Value

BelongsTo

at line 312
QueriesRelationships orHasMorph(MorphTo|string $relation, string|array $types, string $operator = '>=', Expression|int $count = 1)

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

Parameters

MorphTo|string $relation
string|array $types
string $operator
Expression|int $count

Return Value

QueriesRelationships

at line 326
QueriesRelationships doesntHaveMorph(MorphTo|string $relation, string|array $types, string $boolean = 'and', Closure|null $callback = null)

Add a polymorphic relationship count / exists condition to the query.

Parameters

MorphTo|string $relation
string|array $types
string $boolean
Closure|null $callback

Return Value

QueriesRelationships

at line 337
QueriesRelationships orDoesntHaveMorph(MorphTo|string $relation, string|array $types)

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

Parameters

MorphTo|string $relation
string|array $types

Return Value

QueriesRelationships

at line 351
QueriesRelationships whereHasMorph(MorphTo|string $relation, string|array $types, Closure|null $callback = null, string $operator = '>=', Expression|int $count = 1)

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

Parameters

MorphTo|string $relation
string|array $types
Closure|null $callback
string $operator
Expression|int $count

Return Value

QueriesRelationships

at line 365
QueriesRelationships orWhereHasMorph(MorphTo|string $relation, string|array $types, Closure|null $callback = null, string $operator = '>=', Expression|int $count = 1)

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

Parameters

MorphTo|string $relation
string|array $types
Closure|null $callback
string $operator
Expression|int $count

Return Value

QueriesRelationships

at line 379
QueriesRelationships whereDoesntHaveMorph(MorphTo|string $relation, string|array $types, Closure|null $callback = null)

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

Parameters

MorphTo|string $relation
string|array $types
Closure|null $callback

Return Value

QueriesRelationships

at line 393
QueriesRelationships orWhereDoesntHaveMorph(MorphTo|string $relation, string|array $types, Closure|null $callback = null)

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

Parameters

MorphTo|string $relation
string|array $types
Closure|null $callback

Return Value

QueriesRelationships

at line 406
QueriesRelationships whereRelation(Relation|string $relation, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a basic where clause to a relationship query.

Parameters

Relation|string $relation
Closure|string|array|Expression $column
mixed $operator
mixed $value

Return Value

QueriesRelationships

at line 422
QueriesRelationships withWhereRelation(Relation|string $relation, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a basic where clause to a relationship query and eager-load the relationship with the same conditions.

Parameters

Relation|string $relation
Closure|string|array|Expression $column
mixed $operator
mixed $value

Return Value

QueriesRelationships

at line 440
QueriesRelationships orWhereRelation(Relation|string $relation, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add an "or where" clause to a relationship query.

Parameters

Relation|string $relation
Closure|string|array|Expression $column
mixed $operator
mixed $value

Return Value

QueriesRelationships

at line 459
QueriesRelationships whereDoesntHaveRelation(Relation|string $relation, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a basic count / exists condition to a relationship query.

Parameters

Relation|string $relation
Closure|string|array|Expression $column
mixed $operator
mixed $value

Return Value

QueriesRelationships

at line 478
QueriesRelationships orWhereDoesntHaveRelation(Relation|string $relation, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add an "or where" clause to a relationship query.

Parameters

Relation|string $relation
Closure|string|array|Expression $column
mixed $operator
mixed $value

Return Value

QueriesRelationships

at line 498
QueriesRelationships whereMorphRelation(MorphTo|string $relation, string|array $types, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a polymorphic relationship condition to the query with a where clause.

Parameters

MorphTo|string $relation
string|array $types
Closure|string|array|Expression $column
mixed $operator
mixed $value

Return Value

QueriesRelationships

at line 514
QueriesRelationships orWhereMorphRelation(MorphTo|string $relation, string|array $types, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a polymorphic relationship condition to the query with an "or where" clause.

Parameters

MorphTo|string $relation
string|array $types
Closure|string|array|Expression $column
mixed $operator
mixed $value

Return Value

QueriesRelationships

at line 530
QueriesRelationships whereMorphDoesntHaveRelation(MorphTo|string $relation, string|array $types, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a polymorphic relationship condition to the query with a doesn't have clause.

Parameters

MorphTo|string $relation
string|array $types
Closure|string|array|Expression $column
mixed $operator
mixed $value

Return Value

QueriesRelationships

at line 546
QueriesRelationships orWhereMorphDoesntHaveRelation(MorphTo|string $relation, string|array $types, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a polymorphic relationship condition to the query with an "or doesn't have" clause.

Parameters

MorphTo|string $relation
string|array $types
Closure|string|array|Expression $column
mixed $operator
mixed $value

Return Value

QueriesRelationships

at line 559
QueriesRelationships whereMorphedTo(MorphTo|string $relation, mixed $model, string $boolean = 'and')

Add a morph-to relationship condition to the query.

Parameters

MorphTo|string $relation
mixed $model
string $boolean

Return Value

QueriesRelationships

at line 605
QueriesRelationships whereNotMorphedTo(MorphTo|string $relation, mixed $model, string $boolean = 'and')

Add a not morph-to relationship condition to the query.

Parameters

MorphTo|string $relation
mixed $model
string $boolean

Return Value

QueriesRelationships

at line 649
QueriesRelationships orWhereMorphedTo(MorphTo|string $relation, mixed $model)

Add a morph-to relationship condition to the query with an "or where" clause.

Parameters

MorphTo|string $relation
mixed $model

Return Value

QueriesRelationships

at line 660
QueriesRelationships orWhereNotMorphedTo(MorphTo|string $relation, mixed $model)

Add a not morph-to relationship condition to the query with an "or where" clause.

Parameters

MorphTo|string $relation
mixed $model

Return Value

QueriesRelationships

at line 672
QueriesRelationships whereBelongsTo(mixed $related, string|null $relationshipName = null, string $boolean = 'and')

Add a "belongs to" relationship where clause to the query.

Parameters

mixed $related
string|null $relationshipName
string $boolean

Return Value

QueriesRelationships

Exceptions

RelationNotFoundException

at line 714
QueriesRelationships orWhereBelongsTo(mixed $related, string|null $relationshipName = null)

Add a "BelongsTo" relationship with an "or where" clause to the query.

Parameters

mixed $related
string|null $relationshipName

Return Value

QueriesRelationships

Exceptions

RuntimeException

at line 726
QueriesRelationships whereAttachedTo(mixed $related, string|null $relationshipName = null, string $boolean = 'and')

Add a "belongs to many" relationship where clause to the query.

Parameters

mixed $related
string|null $relationshipName
string $boolean

Return Value

QueriesRelationships

Exceptions

RelationNotFoundException

at line 764
QueriesRelationships orWhereAttachedTo(mixed $related, string|null $relationshipName = null)

Add a "belongs to many" relationship with an "or where" clause to the query.

Parameters

mixed $related
string|null $relationshipName

Return Value

QueriesRelationships

Exceptions

RuntimeException

at line 772
QueriesRelationships withAggregate(mixed $relations, Expression|string $column, string|null $function = null)

Add subselect queries to include an aggregate value for a relationship.

Parameters

mixed $relations
Expression|string $column
string|null $function

Return Value

QueriesRelationships

at line 873
protected string getRelationHashedColumn(string $column, Relation $relation)

Get the relation hashed column name for the given column and relation.

Parameters

string $column
Relation $relation

Return Value

string

at line 887
QueriesRelationships withCount(mixed $relations)

Add subselect queries to count the relations.

Parameters

mixed $relations

Return Value

QueriesRelationships

at line 895
QueriesRelationships withMax(string|array $relation, Expression|string $column)

Add subselect queries to include the max of the relation's column.

Parameters

string|array $relation
Expression|string $column

Return Value

QueriesRelationships

at line 903
QueriesRelationships withMin(string|array $relation, Expression|string $column)

Add subselect queries to include the min of the relation's column.

Parameters

string|array $relation
Expression|string $column

Return Value

QueriesRelationships

at line 911
QueriesRelationships withSum(string|array $relation, Expression|string $column)

Add subselect queries to include the sum of the relation's column.

Parameters

string|array $relation
Expression|string $column

Return Value

QueriesRelationships

at line 919
QueriesRelationships withAvg(string|array $relation, Expression|string $column)

Add subselect queries to include the average of the relation's column.

Parameters

string|array $relation
Expression|string $column

Return Value

QueriesRelationships

at line 927
QueriesRelationships withExists(string|array $relation)

Add subselect queries to include the existence of related models.

Parameters

string|array $relation

Return Value

QueriesRelationships

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

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

Parameters

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

Return Value

QueriesRelationships

at line 952
QueriesRelationships mergeConstraintsFrom(Builder $from)

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

Parameters

Builder $from

Return Value

QueriesRelationships

at line 979
protected array requalifyWhereTables(array $wheres, string $from, string $to)

Updates the table name for any columns with a new qualified name.

Parameters

array $wheres
string $from
string $to

Return Value

array

at line 993
protected QueriesRelationships addWhereCountQuery(Builder $query, string $operator = '>=', Expression|int $count = 1, string $boolean = 'and')

Add a sub-query count clause to this query.

Parameters

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

Return Value

QueriesRelationships

at line 1010
protected Relation getRelationWithoutConstraints(string $relation)

Get the "has relation" base query instance.

Parameters

string $relation

Return Value

Relation

at line 1020
protected bool canUseExistsForExistenceCheck(string $operator, Expression|int $count)

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

Parameters

string $operator
Expression|int $count

Return Value

bool