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

clinvest's avatar

Nested withCount (comments and replies)

Hello all,

I'm working on a blog, with pages > comments > replies to comment

So a page has comments :

public function comments()
    {
        return $this->hasMany(WpComment::class, 'wp_page_id', 'id')->where('status', 'approved')->where('wp_comment_id', 0);
    }

and a comment has replies:

public function replies()
    {
        return $this->hasMany(WpComment::class, 'wp_comment_id')->where('status', 'approved');
    }

Now i'm querying a page, with comments, and I need to count Comments with Replies, but I'm struggling to do it:

WpPage::query()
                    ->select('id', 'title', 'sections', 'header', 'blocks', 'breadcrumb', 'type', 'wp_media_id', 'wp_user_id')
                    ->where(['link' => $pageLink])
                    ->withCount('comments')
                    ->with('comments:id,wp_page_id,author,content' )
                    ->firstOrFail();

For now, I get the count of Comments but without Replies.

How would you do that? do I need to search about "Counting Related Models On Morph To Relationships"?

Thank you in advance,

Cyril

0 likes
6 replies
Sinnbeck's avatar

Do you mean this ?

WpPage::query()
                    ->select('id', 'title', 'sections', 'header', 'blocks', 'breadcrumb', 'type', 'wp_media_id', 'wp_user_id')
                    ->where(['link' => $pageLink])
                    ->withCount(['comments' => fn($query) => $query->has('replies')])
                    ->with('comments:id,wp_page_id,author,content' )
                    ->firstOrFail();
1 like
clinvest's avatar

@Sinnbeck thank you for your fast answer :)

How to count (sum) comments & replies? tried your code and it seems to count only replies.

Thank you in advance,

Sinnbeck's avatar

@clinvest what do you mean? You want to add the reply and comments count into 1 number?

MohamedTammam's avatar

In your page define hasManyThrough relationship

public function replies()
{
	return $this->hasManyThrough(WpComment::class, WpComment::class, 'wp_page_id', 'wp_comment_id', 'id', 'id');
}

https://laravel.com/docs/9.x/eloquent-relationships#has-many-through

Then to get all replies on pages

WpPage::query()
	->withCount(['comments', 'replies'])

https://stackoverflow.com/questions/39633691/laravel-5-3-withcount-nested-relation

I'm just curious, does your Laravel work good with WordPress? is it an opensource?

clinvest's avatar

@MohamedTammam thank you for your answer. Well I have a Not unique table/alias: 'wp_comments' error when I test your solution, I guess it doesn't like the WpComment::class, WpComment::class.

Well, I sync my Wordpress posts & comments in my Laravel database using the wordpress rest api, so in fact, after the data is synchronised, it's pure Laravel, and my plan is to change Wp for Laravel, because I need extra features and for performance reason. It's not open source. Hope it answers your question :)

1 like

Please or to participate in this conversation.