1

I have a system of commenting models driven by the Laralvel-Commentable package

Unfortunately I am underskilled in PHP for the job of implementing nested comments (replies).

Problem : adding a reply:

When I used hints from this thread: Laravel 5: how to do multi threaded comments

And added this code to the create method:

    $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);

Note: in the reply form I have a hidden input

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

After submitting the form I get this error:

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

Thing to do: make the thing work properly. I have no clue! Sorry.

1
  • The package laravel-commentable is now deprecated. Commented Sep 13, 2016 at 3:54

1 Answer 1

1

You have to do

$comment->save();

before trying to move the new comment into a child position. So in other words you should do this:

$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

When you save the comment object it gets an id from your database and that id is used to relate to the parent - child relationship

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.