使用 Laravel-可评论

using Laravel-Commentable

我有一个由 Laralvel-Commentable 包裹

不幸的是,我在 PHP 执行嵌套评论(回复)方面的技能不足。

问题:添加回复:

当我使用此线程中的提示时:

并将此代码添加到 create 方法中:

    $object = Lead::find($input['item_id']);
    $comment = new Comment;
    $comment->body = $input['comment'];
    $comment->user_id = Auth::id();
// added section
    if(isset($input['parent_id'])) {
        $comment->makeChildOf($input['parent_id']);
    }
// end of added section

    $object->comments()->save($comment);

注意:在回复表单中我有一个隐藏的输入

                {!! Form::hidden('parent_id', $o->id) !!}

提交表单后出现此错误:

MoveNotPossibleException in Move.php line 198: A new node cannot be moved.

要做的事:使它正常工作。我没有线索!对不起。

你必须做

$comment->save();

在尝试将新评论移至子位置之前。所以换句话说,你应该这样做:

$comment = new Comment;
$comment->body = $input['comment'];
$comment->user_id = Auth::id();
// Save first
$comment->save();

// added section
if(isset($input['parent_id'])) {
    $comment->makeChildOf($input['parent_id']);
}
// end of added section

当您保存评论对象时,它会从您的数据库中获取一个 ID,该 ID 用于关联父子关系