From b44b4e309e4595f2926f3b3a1abfa883395e9f57 Mon Sep 17 00:00:00 2001 From: manukminasyan Date: Fri, 27 Mar 2026 21:20:20 +0400 Subject: [PATCH] fix: avoid lazy loading parent relationship in depth calculation Use a query-based approach instead of traversing the parent relationship to prevent LazyLoadingViolationException when strict mode is enabled. --- src/Models/Comment.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Models/Comment.php b/src/Models/Comment.php index f519ecf..eb3714d 100644 --- a/src/Models/Comment.php +++ b/src/Models/Comment.php @@ -130,15 +130,12 @@ class Comment extends Model public function depth(): int { $depth = 0; - $comment = $this; + $maxDepth = CommentsConfig::getMaxDepth(); + $parentId = $this->parent_id; - while ($comment->parent_id !== null) { - $comment = $comment->parent; + while ($parentId !== null && $depth < $maxDepth) { $depth++; - - if ($depth >= CommentsConfig::getMaxDepth()) { - return CommentsConfig::getMaxDepth(); - } + $parentId = static::where('id', $parentId)->value('parent_id'); } return $depth;