0

I have a topic that contain comments and each comment can contain replies. However, I want to optimize the way I count the total comments of each topics (comment + reply).

In my Topic model, I can optimally get the comment count using this syntax:

$this->comments()->count();

but since I have to get the replies, I have to iterate each comment so I can get the total value. This is not optimized because the custom attributes of the comments are also processed.

Is there any optimized way I can get the total comments and its replies from a topic without iterating each comments?

  • There are multiple "optimized ways" to do that. If you only need the comments, maybe you need to use the Comment model and include the related Topic instead of querying on the Topics. – Gert B. Sep 16 '21 at 08:51
  • I believe you can do $this->with("comments.replies')->get() and use that one response data to count the comments and replies. (assuming replies is the relationship name) – Cameron Sep 16 '21 at 13:04

0 Answers0