Skip to content

How to get count of child records in hasMany relationship #7345

Discussion options

You must be logged in to vote

@HarshalNathe ,

Indeed that /count path is not created by default. But you can add it easily as follows: I will take the todo-list example as the basis for my explanation. The key part here is that the count method resides in the child repository. The controller that contains the relationship is todo-list-todo.controller.ts so we need to import the repository of the child first, since the constrained relation repository does not have this method available for now.

Also the count method from the repository receives a Where parameter, so we need to force the parent ID, so it behaves similar to other /count in regular controllers.

Step 1:
Import the repository of the child in the relationshi…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@HarshalNathe
Comment options

@marioestradarosa
Comment options

@HarshalNathe
Comment options

@marioestradarosa
Comment options

Answer selected by achrinza
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants