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.
This commit is contained in:
manukminasyan
2026-03-27 21:20:20 +04:00
parent ac97dcb092
commit b44b4e309e

View File

@@ -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;