Querying Relationships
When querying an entity, you may want to restrict the query based on the existence or absence of a related entity. You can do that using the following four methods:
has
Checks for the existence of a relationship when executing the query.
By default, a has
constraint will only return entities that have one or more of the related entity.
An optional operator and count can be added to the call.
Nested relationships can be checked by passing a dot-delimited string of relationships.
doesntHave
Checks for the absence of a relationship when executing the query.
By default, a doesntHave
constraint will only return entities that have zero of the related entity.
An optional operator and count can be added to the call.
Nested relationships can be checked by passing a dot-delimited string of relationships.
whereHas
When you need to have more control over the relationship constraint, you can use whereHas
. This method operates similarly to has
but also accepts a callback to configure the relationship constraint.
The whereHas
callback is passed a builder instance configured according to the relationship. You may call any entity or query builder methods on it as usual.
When you specify a nested relationship, the builder instance is configured for the last relationship specified.
An optional operator and count can be added to the call, as well.
whereDoesntHave
The whereDoesntHave
callback is passed a builder instance configured according to the relationship. You may call any entity or query builder methods on it as usual.
When you specify a nested relationship, the builder instance is configured for the last relationship specified.
An optional operator and count can be added to the call, as well.