Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Batman55's avatar
Level 32

return a comments_spam_count property in this query

I am following along with Andre Madarang in Build a Voting App. I would like to add a property that returns like comments_count toward the bottom called comments_spam_count.

There is a table comments with a field spam_reports that holds the number of times the comment has been reported as spam. I would like to return the number of spam reports per idea. I for the life of me have not been able to figure this out.

Comments table has field idea_id that is a foreign key to table idea field id.

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To add a comments_spam_count property to your query, you can use the withCount method with a condition. This will allow you to count only the comments that have been reported as spam for each idea. Here's how you can modify your query to include this:

In this solution, the withCount method is used with a closure to count only the comments where spam_reports is greater than zero. This adds a comments_spam_count attribute to each Idea model in the result set, which represents the number of spam reports for comments associated with that idea.

Please or to participate in this conversation.