From 812556cba20e6cc7ef8980378bf9785a74ff4041 Mon Sep 17 00:00:00 2001 From: ilyapashayan Date: Mon, 30 Mar 2026 18:59:07 +0400 Subject: [PATCH] fix: include replies in comment count and cascade delete to replies --- resources/views/livewire/comments.blade.php | 2 +- src/Livewire/Comments.php | 8 +++++++- src/Models/Comment.php | 4 ++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/resources/views/livewire/comments.blade.php b/resources/views/livewire/comments.blade.php index 3764b17..19ddf14 100644 --- a/resources/views/livewire/comments.blade.php +++ b/resources/views/livewire/comments.blade.php @@ -6,7 +6,7 @@ {{-- Sort toggle --}}

- Comments ({{ $this->totalCount }}) + Comments ({{ $this->allCommentsCount }})

@auth
diff --git a/src/Livewire/Comments.php b/src/Livewire/Comments.php index bfbe6bc..da391de 100644 --- a/src/Livewire/Comments.php +++ b/src/Livewire/Comments.php @@ -80,6 +80,12 @@ class Comments extends Component implements HasForms return $this->model->topLevelComments()->count(); } + #[Computed] + public function allCommentsCount(): int + { + return $this->model->commentCount(); + } + #[Computed] public function hasMore(): bool { @@ -203,7 +209,7 @@ class Comments extends Component implements HasForms public function refreshComments(): void { - unset($this->comments, $this->totalCount, $this->hasMore); + unset($this->comments, $this->totalCount, $this->hasMore, $this->allCommentsCount); } public function render(): View diff --git a/src/Models/Comment.php b/src/Models/Comment.php index eb3714d..48eb5f5 100644 --- a/src/Models/Comment.php +++ b/src/Models/Comment.php @@ -28,6 +28,10 @@ class Comment extends Model $comment->body = Str::sanitizeHtml($comment->body); }); + static::deleting(function (self $comment): void { + $comment->replies()->each(fn ($reply) => $reply->delete()); + }); + static::forceDeleting(function (self $comment): void { $comment->attachments()->delete(); $comment->reactions()->delete();