我正在尝试使用Laravel CommentableBaum结合使用嵌套集来实现多线程注释

我设法使根注释生效,但是当我回复注释时,数据库中的记录被插入而没有commentable_idcommentable_type,因此无法知道对该注释的回复是针对App\Post还是App\Product,因为这两个字段是空的,我似乎不明白为什么。

table

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()进行注释之前先让 child 成为 child ,所以在它用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)所做的更改会被抛出。

关于php - Baum和Laravel的嵌套集,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33104394/

10-12 05:34