带有Baum和Laravel Commentable的嵌套集:插入没有Commentable id和类型的子注释


Nested sets with Baum and Laravel Commentable: children comments are being inserted without the commentable id and type

我正在尝试使用Laravel Commentable实现多线程注释,它使用了Baum的嵌套集

我已经设法使根评论工作,但是当我回复评论时,数据库中的记录在没有commentable_idcommentable_type的情况下插入,所以没有办法知道对该评论的回复是否适用于App'PostApp'Product,因为这2个字段是空的,我似乎无法理解为什么。

users: id, name, email...
posts: id, user_id, subreddit_id...
comments: id, user_id, parent_id, lft, rgt, depth, commentable_id, commentable_type

路线

Route::post('comments/{post}', ['as' => 'comment', 'uses' => 'PostsController@createComment']);
Route::post('comments/{comment}/child', ['as' => 'child-comment', 'uses' => 'PostsController@createChildComment']);

PostController中的方法

public function createComment($id) {
    $post = Post::with('user.votes')->with('subreddit.moderators')->where('id', $id)->first();
    $comment = new Comment;
    $comment->body = Input::get('comment');
    $comment->user_id = Auth::id();
    $post->comments()->save($comment);
}
public function createChildComment(Post $post){
    $parent = Comment::find(Input::get('parent_id'));
    $comment = new Comment;
    $comment->body = Input::get('child-comment');
    $comment->user_id = Auth::id();
    $comment->save();
    $comment->makeChildOf($parent);
}

查看根注释和子注释

<-- This is for root comments --/>
{!! Form::open(['route' => ['comment', $post]]) !!}
@foreach($comments as $comment)
@endforeach
{!! Form::close() !!}
<-- This is for children comments --/>
{!! Form::open(['route' => ['child-comment', $comment]]) !!}
<input type="hidden" name="parent_id" value="{{ $comment->id }}"/>
{!! Form::close() !!}

在我的脑海中,你不会让孩子之前你$comment->save()的评论,所以它在正确的状态之前,它击中数据库与save

编辑:试试这个:

public function createChildComment(Post $post){
    $parent = Comment::find(Input::get('parent_id'));
    $comment = new Comment;
    $comment->body = Input::get('child-comment');
    $comment->user_id = Auth::id();
    $comment->save();
    $comment->makeChildOf($parent);
    $comment->save();
}

目前我相信$comment->makeChildOf($parent)所做的改变会被抛出。