trait QueriesRelationships (View source)

Methods

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

Add a relationship count / exists condition to the query.

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

Add nested relationship count / exists conditions to the query.

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

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

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

Add a relationship count / exists condition to the query.

orDoesntHave(string $relation)

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

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

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

orWhereHas(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".

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

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

orWhereDoesntHave(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 = '>=', 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 = '>=', 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 = '>=', 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 = '>=', 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(string $relation, Closure|string|array|Expression $column, mixed $operator = null, mixed $value = null)

Add a basic where clause to a relationship query.

orWhereRelation(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.

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

Add a morph-to relationship condition to the query.

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

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

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

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

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

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

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

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

$this
withCount(mixed $relations)

Add subselect queries to count the relations.

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

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

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

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

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

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

$this
withAvg(string|array $relation, 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.

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

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

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

Details

Builder|QueriesRelationships has(Relation|string $relation, string $operator = '>=', 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
int $count
string $boolean
Closure|null $callback

Return Value

Builder|QueriesRelationships

Exceptions

RuntimeException

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

Return Value

Builder|QueriesRelationships

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

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

Parameters

string $relation
string $operator
int $count

Return Value

Builder|QueriesRelationships

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

Add a relationship count / exists condition to the query.

Parameters

string $relation
string $boolean
Closure|null $callback

Return Value

Builder|QueriesRelationships

Builder|QueriesRelationships orDoesntHave(string $relation)

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

Parameters

string $relation

Return Value

Builder|QueriesRelationships

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

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

Parameters

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

Return Value

Builder|QueriesRelationships

Builder|QueriesRelationships orWhereHas(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

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

Return Value

Builder|QueriesRelationships

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

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

Parameters

string $relation
Closure|null $callback

Return Value

Builder|QueriesRelationships

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

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

Parameters

string $relation
Closure|null $callback

Return Value

Builder|QueriesRelationships

Builder|QueriesRelationships hasMorph(MorphTo|string $relation, string|array $types, string $operator = '>=', 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
int $count
string $boolean
Closure|null $callback

Return Value

Builder|QueriesRelationships

protected BelongsTo getBelongsToRelation(MorphTo $relation, string $type)

Get the BelongsTo relationship for a single polymorphic type.

Parameters

MorphTo $relation
string $type

Return Value

BelongsTo

Builder|QueriesRelationships orHasMorph(MorphTo|string $relation, string|array $types, string $operator = '>=', 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
int $count

Return Value

Builder|QueriesRelationships

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

Builder|QueriesRelationships

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

Builder|QueriesRelationships

Builder|QueriesRelationships whereHasMorph(MorphTo|string $relation, string|array $types, Closure|null $callback = null, string $operator = '>=', 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
int $count

Return Value

Builder|QueriesRelationships

Builder|QueriesRelationships orWhereHasMorph(MorphTo|string $relation, string|array $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

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

Return Value

Builder|QueriesRelationships

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

Builder|QueriesRelationships

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

Builder|QueriesRelationships

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

Add a basic where clause to a relationship query.

Parameters

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

Return Value

Builder|QueriesRelationships

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

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

Parameters

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

Return Value

Builder|QueriesRelationships

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

Builder|QueriesRelationships

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

Builder|QueriesRelationships

Builder|QueriesRelationships whereMorphedTo(MorphTo|string $relation, Model|string $model, $boolean = 'and')

Add a morph-to relationship condition to the query.

Parameters

MorphTo|string $relation
Model|string $model
$boolean

Return Value

Builder|QueriesRelationships

Builder|QueriesRelationships orWhereMorphedTo(MorphTo|string $relation, Model|string $model)

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

Parameters

MorphTo|string $relation
Model|string $model

Return Value

Builder|QueriesRelationships

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

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

Parameters

Model $related
$relationshipName
string $boolean

Return Value

$this

Exceptions

RuntimeException

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

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

Parameters

Model $related
$relationshipName

Return Value

$this

Exceptions

RuntimeException

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

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

Parameters

mixed $relations
string $column
string $function

Return Value

$this

$this withCount(mixed $relations)

Add subselect queries to count the relations.

Parameters

mixed $relations

Return Value

$this

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

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

Parameters

string|array $relation
string $column

Return Value

$this

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

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

Parameters

string|array $relation
string $column

Return Value

$this

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

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

Parameters

string|array $relation
string $column

Return Value

$this

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

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

Parameters

string|array $relation
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 Builder|QueriesRelationships 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

Builder|QueriesRelationships

Builder|QueriesRelationships mergeConstraintsFrom(Builder $from)

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

Parameters

Builder $from

Return Value

Builder|QueriesRelationships

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 getRelationWithoutConstraints(string $relation)

Get the "has relation" base query instance.

Parameters

string $relation

Return Value

Relation

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.