Skip to content

Commit 928bd12

Browse files
Merge pull request #73 from MetaFilter/70-fix-comment-count
Fix comment count to match # of comments
2 parents 696199d + 952b5e2 commit 928bd12

5 files changed

Lines changed: 10 additions & 4 deletions

File tree

app/Livewire/Posts/PostIndexItemComponent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function mount(Post $post): void
2121
{
2222
$this->authorizedUserId = $this->getAuthorizedUserId();
2323
$this->post = $post;
24-
$this->commentCount = $post->comments()->count();
24+
$this->commentCount = $post->commentsCount();
2525
}
2626

2727
public function render(): View

app/Models/Post.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ public function comments(): HasMany
143143
return $this->hasMany(Comment::class);
144144
}
145145

146+
public function commentsCount(): int
147+
{
148+
// Only count comments by regular users, not moderator actions
149+
return $this->hasMany(Comment::class)->whereNull('moderation_type')->count();
150+
}
151+
146152
public function bookmarkCount(): int
147153
{
148154
return Bookmark::count($this);

resources/views/posts/partials/post-index-footer.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
href="{{ $post->present()->url }}#comments"
1010
title="{{ trans('Comments') }}">
1111
<x-icons.icon-component filename="chat" />
12-
{{ $post->comments()->count() > 0 ?: 0 }}
12+
{{ $post->commentsCount() }}
1313
</a>
1414
</footer>

resources/views/posts/partials/post-show-footer.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<span>
1717
<x-icons.icon-component filename="chat" />
18-
{{ $commentsCount > 0 ?: 0 }}
18+
{{ $commentsCount }}
1919
</span>
2020

2121
@if (isset($favoritesCount) && $favoritesCount > 0)

resources/views/posts/show.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
@include('posts.partials.post-show-footer', [
4444
'post' => $post,
45-
'commentsCount' => $post->comments()->count(),
45+
'commentsCount' => $post->commentsCount(),
4646
'favoritesCount' => $post->favoriteCount(),
4747
])
4848
</article>

0 commit comments

Comments
 (0)