trait QueriesRelationships mixin Builder (View source)

Methods

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

Add a relationship count / exists condition to the query.

$this
hasNested(string $relations, string $operator = '>=', int $count = 1, string $boolean = 'and', Builder<mixed>): mixed|null $callback = null)

Add nested relationship count / exists conditions to the query.

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

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

$this
doesntHave(TRelatedModel, mixed, mixed>|string $relation, string $boolean = 'and', Closure|null $callback = null)

Add a relationship count / exists condition to the query.

$this
orDoesntHave(Relation<mixed, mixed, mixed>|string $relation)

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

$this
whereHas(TRelatedModel, mixed, mixed>|string $relation, Closure|null $callback = null, string $operator = '>=', int $count = 1)

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

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

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

$this
orWhereHas(TRelatedModel, mixed, mixed>|string $relation, Closure|null $callback = null, string $operator = '>=', int $count = 1)

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

$this
whereDoesntHave(TRelatedModel, mixed, mixed>|string $relation, Closure|null $callback = null)

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

$this
orWhereDoesntHave(TRelatedModel, mixed, mixed>|string $relation, Closure|null $callback = null)

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

$this
hasMorph(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, string $operator = '>=', int $count = 1, string $boolean = 'and', Closure|null $callback = null)

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

TDeclaringModel>
getBelongsToRelation(MorphTo $relation, TRelatedModel> $type)

Get the BelongsTo relationship for a single polymorphic type.

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

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

$this
doesntHaveMorph(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, string $boolean = 'and', Closure|null $callback = null)

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

$this
orDoesntHaveMorph(MorphTo<mixed, mixed>|string $relation, string|array<int, string> $types)

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

$this
whereHasMorph(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, Closure|null $callback = null, string $operator = '>=', int $count = 1)

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

$this
orWhereHasMorph(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, Closure|null $callback = null, string $operator = '>=', int $count = 1)

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

$this
whereDoesntHaveMorph(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, Closure|null $callback = null)

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

$this
orWhereDoesntHaveMorph(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, Closure|null $callback = null)

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

$this
whereRelation(TRelatedModel, mixed, mixed>|string $relation, TRelatedModel>): mixed|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a basic where clause to a relationship query.

$this
withWhereRelation(Relation<mixed, mixed, mixed>|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.

$this
orWhereRelation(TRelatedModel, mixed, mixed>|string $relation, TRelatedModel>): mixed|string|array|Expression $column, mixed $operator = null, mixed $value = null)

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

$this
whereDoesntHaveRelation(TRelatedModel, mixed, mixed>|string $relation, TRelatedModel>): mixed|string|array|Expression $column, mixed $operator = null, mixed $value = null)

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

$this
orWhereDoesntHaveRelation(TRelatedModel, mixed, mixed>|string $relation, TRelatedModel>): mixed|string|array|Expression $column, mixed $operator = null, mixed $value = null)

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

$this
whereMorphRelation(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, TRelatedModel>): mixed|string|array|Expression $column, mixed $operator = null, mixed $value = null)

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

$this
orWhereMorphRelation(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, TRelatedModel>): mixed|string|array|Expression $column, mixed $operator = null, mixed $value = null)

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

$this
whereMorphDoesntHaveRelation(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, TRelatedModel>): mixed|string|array|Expression $column, mixed $operator = null, mixed $value = null)

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

$this
orWhereMorphDoesntHaveRelation(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, TRelatedModel>): mixed|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.

$this
whereMorphedTo(MorphTo<mixed, mixed>|string $relation, Model|Model>|string|null $model, $boolean = 'and')

Add a morph-to relationship condition to the query.

$this
whereNotMorphedTo(MorphTo<mixed, mixed>|string $relation, Model|Model>|string $model, $boolean = 'and')

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

$this
orWhereMorphedTo(MorphTo<mixed, mixed>|string $relation, Model|Model>|string|null $model)

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

$this
orWhereNotMorphedTo(MorphTo<mixed, mixed>|string $relation, Model|Model>|string $model)

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

$this
whereBelongsTo(Model|Model> $related, string|null $relationshipName = null, string $boolean = 'and')

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

$this
orWhereBelongsTo(Model $related, string|null $relationshipName = null)

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

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

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

string
getRelationHashedColumn(string $column, Relation<mixed, mixed, mixed> $relation)

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

$this
withCount(mixed $relations)

Add subselect queries to count the relations.

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

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

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

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

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

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

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

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

$this
withExists(string|array $relation)

Add subselect queries to include the existence of related models.

$this
addHasWhere(Builder $hasQuery, Relation $relation, string $operator, int $count, string $boolean)

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

$this
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.

$this
addWhereCountQuery(Builder $query, string $operator = '>=', int $count = 1, string $boolean = 'and')

Add a sub-query count clause to this query.

Relation<mixed, mixed, mixed>
getRelationWithoutConstraints(string $relation)

Get the "has relation" base query instance.

bool
canUseExistsForExistenceCheck(string $operator, int $count)

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

Details

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

Add a relationship count / exists condition to the query.

Parameters

TRelatedModel, mixed, mixed>|string $relation
string $operator
int $count
string $boolean
Closure|null $callback

Return Value

$this

Exceptions

RuntimeException

protected $this hasNested(string $relations, string $operator = '>=', int $count = 1, string $boolean = 'and', Builder<mixed>): mixed|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
int $count
string $boolean
Builder<mixed>): mixed|null $callback

Return Value

$this

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

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

Parameters

Relation<mixed, mixed, mixed>|string $relation
string $operator
int $count

Return Value

$this

$this doesntHave(TRelatedModel, mixed, mixed>|string $relation, string $boolean = 'and', Closure|null $callback = null)

Add a relationship count / exists condition to the query.

Parameters

TRelatedModel, mixed, mixed>|string $relation
string $boolean
Closure|null $callback

Return Value

$this

$this orDoesntHave(Relation<mixed, mixed, mixed>|string $relation)

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

Parameters

Relation<mixed, mixed, mixed>|string $relation

Return Value

$this

$this whereHas(TRelatedModel, mixed, mixed>|string $relation, Closure|null $callback = null, string $operator = '>=', int $count = 1)

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

Parameters

TRelatedModel, mixed, mixed>|string $relation
Closure|null $callback
string $operator
int $count

Return Value

$this

$this withWhereHas(string $relation, Closure|null $callback = null, string $operator = '>=', 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
int $count

Return Value

$this

$this orWhereHas(TRelatedModel, mixed, mixed>|string $relation, Closure|null $callback = null, string $operator = '>=', int $count = 1)

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

Parameters

TRelatedModel, mixed, mixed>|string $relation
Closure|null $callback
string $operator
int $count

Return Value

$this

$this whereDoesntHave(TRelatedModel, mixed, mixed>|string $relation, Closure|null $callback = null)

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

Parameters

TRelatedModel, mixed, mixed>|string $relation
Closure|null $callback

Return Value

$this

$this orWhereDoesntHave(TRelatedModel, mixed, mixed>|string $relation, Closure|null $callback = null)

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

Parameters

TRelatedModel, mixed, mixed>|string $relation
Closure|null $callback

Return Value

$this

$this hasMorph(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, string $operator = '>=', int $count = 1, string $boolean = 'and', Closure|null $callback = null)

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

Parameters

TRelatedModel, mixed>|string $relation
string|array<int, string> $types
string $operator
int $count
string $boolean
Closure|null $callback

Return Value

$this

protected TDeclaringModel> getBelongsToRelation(MorphTo $relation, TRelatedModel> $type)

Get the BelongsTo relationship for a single polymorphic type.

Parameters

MorphTo $relation
TRelatedModel> $type

Return Value

TDeclaringModel>

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

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

Parameters

MorphTo<mixed, mixed>|string $relation
string|array<int, string> $types
string $operator
int $count

Return Value

$this

$this doesntHaveMorph(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, string $boolean = 'and', Closure|null $callback = null)

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

Parameters

TRelatedModel, mixed>|string $relation
string|array<int, string> $types
string $boolean
Closure|null $callback

Return Value

$this

$this orDoesntHaveMorph(MorphTo<mixed, mixed>|string $relation, string|array<int, string> $types)

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

Parameters

MorphTo<mixed, mixed>|string $relation
string|array<int, string> $types

Return Value

$this

$this whereHasMorph(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, Closure|null $callback = null, string $operator = '>=', int $count = 1)

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

Parameters

TRelatedModel, mixed>|string $relation
string|array<int, string> $types
Closure|null $callback
string $operator
int $count

Return Value

$this

$this orWhereHasMorph(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, Closure|null $callback = null, string $operator = '>=', int $count = 1)

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

Parameters

TRelatedModel, mixed>|string $relation
string|array<int, string> $types
Closure|null $callback
string $operator
int $count

Return Value

$this

$this whereDoesntHaveMorph(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, Closure|null $callback = null)

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

Parameters

TRelatedModel, mixed>|string $relation
string|array<int, string> $types
Closure|null $callback

Return Value

$this

$this orWhereDoesntHaveMorph(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, Closure|null $callback = null)

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

Parameters

TRelatedModel, mixed>|string $relation
string|array<int, string> $types
Closure|null $callback

Return Value

$this

$this whereRelation(TRelatedModel, mixed, mixed>|string $relation, TRelatedModel>): mixed|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a basic where clause to a relationship query.

Parameters

TRelatedModel, mixed, mixed>|string $relation
TRelatedModel>): mixed|string|array|Expression $column
mixed $operator
mixed $value

Return Value

$this

$this withWhereRelation(Relation<mixed, mixed, mixed>|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<mixed, mixed, mixed>|string $relation
Closure|string|array|Expression $column
mixed $operator
mixed $value

Return Value

$this

$this orWhereRelation(TRelatedModel, mixed, mixed>|string $relation, TRelatedModel>): mixed|string|array|Expression $column, mixed $operator = null, mixed $value = null)

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

Parameters

TRelatedModel, mixed, mixed>|string $relation
TRelatedModel>): mixed|string|array|Expression $column
mixed $operator
mixed $value

Return Value

$this

$this whereDoesntHaveRelation(TRelatedModel, mixed, mixed>|string $relation, TRelatedModel>): mixed|string|array|Expression $column, mixed $operator = null, mixed $value = null)

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

Parameters

TRelatedModel, mixed, mixed>|string $relation
TRelatedModel>): mixed|string|array|Expression $column
mixed $operator
mixed $value

Return Value

$this

$this orWhereDoesntHaveRelation(TRelatedModel, mixed, mixed>|string $relation, TRelatedModel>): mixed|string|array|Expression $column, mixed $operator = null, mixed $value = null)

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

Parameters

TRelatedModel, mixed, mixed>|string $relation
TRelatedModel>): mixed|string|array|Expression $column
mixed $operator
mixed $value

Return Value

$this

$this whereMorphRelation(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, TRelatedModel>): mixed|string|array|Expression $column, mixed $operator = null, mixed $value = null)

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

Parameters

TRelatedModel, mixed>|string $relation
string|array<int, string> $types
TRelatedModel>): mixed|string|array|Expression $column
mixed $operator
mixed $value

Return Value

$this

$this orWhereMorphRelation(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, TRelatedModel>): mixed|string|array|Expression $column, mixed $operator = null, mixed $value = null)

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

Parameters

TRelatedModel, mixed>|string $relation
string|array<int, string> $types
TRelatedModel>): mixed|string|array|Expression $column
mixed $operator
mixed $value

Return Value

$this

$this whereMorphDoesntHaveRelation(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, TRelatedModel>): mixed|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

TRelatedModel, mixed>|string $relation
string|array<int, string> $types
TRelatedModel>): mixed|string|array|Expression $column
mixed $operator
mixed $value

Return Value

$this

$this orWhereMorphDoesntHaveRelation(TRelatedModel, mixed>|string $relation, string|array<int, string> $types, TRelatedModel>): mixed|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

TRelatedModel, mixed>|string $relation
string|array<int, string> $types
TRelatedModel>): mixed|string|array|Expression $column
mixed $operator
mixed $value

Return Value

$this

$this whereMorphedTo(MorphTo<mixed, mixed>|string $relation, Model|Model>|string|null $model, $boolean = 'and')

Add a morph-to relationship condition to the query.

Parameters

MorphTo<mixed, mixed>|string $relation
Model|Model>|string|null $model
$boolean

Return Value

$this

$this whereNotMorphedTo(MorphTo<mixed, mixed>|string $relation, Model|Model>|string $model, $boolean = 'and')

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

Parameters

MorphTo<mixed, mixed>|string $relation
Model|Model>|string $model
$boolean

Return Value

$this

$this orWhereMorphedTo(MorphTo<mixed, mixed>|string $relation, Model|Model>|string|null $model)

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

Parameters

MorphTo<mixed, mixed>|string $relation
Model|Model>|string|null $model

Return Value

$this

$this orWhereNotMorphedTo(MorphTo<mixed, mixed>|string $relation, Model|Model>|string $model)

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

Parameters

MorphTo<mixed, mixed>|string $relation
Model|Model>|string $model

Return Value

$this

$this whereBelongsTo(Model|Model> $related, string|null $relationshipName = null, string $boolean = 'and')

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

Parameters

Model|Model> $related
string|null $relationshipName
string $boolean

Return Value

$this

Exceptions

RelationNotFoundException

$this orWhereBelongsTo(Model $related, string|null $relationshipName = null)

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

Parameters

Model $related
string|null $relationshipName

Return Value

$this

Exceptions

RuntimeException

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

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

Parameters

mixed $relations
Expression|string $column
string $function

Return Value

$this

protected string getRelationHashedColumn(string $column, Relation<mixed, mixed, mixed> $relation)

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

Parameters

string $column
Relation<mixed, mixed, mixed> $relation

Return Value

string

$this withCount(mixed $relations)

Add subselect queries to count the relations.

Parameters

mixed $relations

Return Value

$this

$this 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

$this

$this 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

$this

$this 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

$this

$this 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

$this

$this withExists(string|array $relation)

Add subselect queries to include the existence of related models.

Parameters

string|array $relation

Return Value

$this

protected $this addHasWhere(Builder $hasQuery, Relation $relation, string $operator, int $count, string $boolean)

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

Parameters

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

Return Value

$this

$this mergeConstraintsFrom(Builder $from)

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

Parameters

Builder $from

Return Value

$this

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

protected $this addWhereCountQuery(Builder $query, string $operator = '>=', int $count = 1, string $boolean = 'and')

Add a sub-query count clause to this query.

Parameters

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

Return Value

$this

protected Relation<mixed, mixed, mixed> getRelationWithoutConstraints(string $relation)

Get the "has relation" base query instance.

Parameters

string $relation

Return Value

Relation<mixed, mixed, mixed>

protected bool canUseExistsForExistenceCheck(string $operator, int $count)

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

Parameters

string $operator
int $count

Return Value

bool

Laravel Cloud is the best place to deploy Laravel, Nuxt, Express, Hono, Node.js, Bun, Go, Flask, Python, and more, with dedicated infrastructure available on AWS through Laravel Private Cloud.