Relationship Aggregates
withCount
One common type of subselect field is the count of related entites. For instance, you may want to load a Post or a list of Posts with the count of Comments on each Post. You can reuse your existing relationship definitions and add this count using the withCount
method.
Adds a count of related entities as a subselect property. Relationships can be constrained at runtime by passing a struct where the key is the relationship name and the value is a function to constrain the query.
Name
Type
Required
Default
Description
relation
any
true
A single relation name or array of relation names to load counts.
By default, you will access the returned count using the relationship name appended with Count
, i.e. comments
will be available under commentsCount
.
You can alias the count attribute using the AS
syntax as follows:
This is especially useful as you can dynamically constrain counts at runtime using the same struct syntax as eager loading with the with
function.
withSum
Adds a sum of an attribute of related entities as a subselect property. Relationships can be constrained at runtime by passing a struct where the key is the relationship name and the value is a function to constrain the query.
Name
Type
Required
Default
Description
relationMapping
any
true
A single relation mapping string (relationName.attributeName
) or array of relation mappings to load as summed subselects.
By default, you will access the returned sum using the relationship name prepended with total
, i.e. purchases.amount
will be available under totalPurchases
.
You can alias the count attribute using the AS
syntax as follows:
This is especially useful as you can dynamically constrain counts at runtime using the same struct syntax as eager loading with the with
function.
Was this helpful?