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:
@@ -130,15 +130,12 @@ class Comment extends Model
|
|||||||
public function depth(): int
|
public function depth(): int
|
||||||
{
|
{
|
||||||
$depth = 0;
|
$depth = 0;
|
||||||
$comment = $this;
|
$maxDepth = CommentsConfig::getMaxDepth();
|
||||||
|
$parentId = $this->parent_id;
|
||||||
|
|
||||||
while ($comment->parent_id !== null) {
|
while ($parentId !== null && $depth < $maxDepth) {
|
||||||
$comment = $comment->parent;
|
|
||||||
$depth++;
|
$depth++;
|
||||||
|
$parentId = static::where('id', $parentId)->value('parent_id');
|
||||||
if ($depth >= CommentsConfig::getMaxDepth()) {
|
|
||||||
return CommentsConfig::getMaxDepth();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $depth;
|
return $depth;
|
||||||
|
|||||||
Reference in New Issue
Block a user