# Relationship Counts

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.

## withCount

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`.

```javascript
var post = getInstance( "Post" )
	.withCount( "comments" )
	.findOrFail( 1 );
	
post.getCommentsCount();
```

You can alias the count attribute using the `AS` syntax as follows:

```javascript
var post = getInstance( "Post" )
	.withCount( "comments AS myCommentsCount" )
	.findOrFail( 1 );
	
post.getMyCommentsCount();
```

This is especially useful as you can dynamically constrain counts at runtime using the same struct syntax as eager loading with the `with` function.

```javascript
var post = getInstance( "Post" )
	.withCount( [
	    "comments AS allCommentsCount",
	    { "comments AS pendingCommentsCount": function( q ) {
	        q.where( "approved", 0 );
	    } },
	    { "comments AS approvedCommentsCount": function( q ) {
	        q.where( "approved", 1 );
	    } }
	] )
	.findOrFail( 1 );

post.getAllCommentsCount();	
post.getPendingCommentsCount();
post.getApprovedCommentsCount();
```

{% hint style="info" %}
Note that where possible it is cleaner and more readable to create a dedicated relationship instead of using dynamic constraints.  In the above example, the `Post` entity could have `pendingComments` and `approvedComments` relationships.  Dynamic constraints are more useful when applying user-provided data to the constraints like searching.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://quick.ortusbooks.com/5.1.0-1/guide/relationships/relationship-counts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
