Peter
Peter

Reputation: 2702

using Laravel-Commentable

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.

Upvotes: 1

Views: 999

Answers (1)

henrik
henrik

Reputation: 1618

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

Upvotes: 1

Related Questions