Laravel 5: how to do multi threaded comments

前端 未结 1 690
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 17:59

I am using Laravel Commentable package which uses Nested Sets pattern with Baum.

I have managed to allow users to make comments on posts but they\'re not threaded, e

1条回答
  •  天涯浪人
    2021-01-16 18:45

    I haven't used Laravel Commentable package, but the docs look pretty good.

    I believe you need use Commentable; on your Post model and not your Comment model.

    It looks like your Comment model needs to extend Baum\Node and not Model

    Then what you have should work.

    $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);
    
    // or you could do 
    
    $comment->makeChildOf($post);
    

    To make a comment on a comment, it looks like you do something like this. I would probably make a CommentsController.

    public function addComment(Request $request){
        $parent = Comment::find(Input::get('parent_id'));
    
        $comment = new Comment;
        $comment->body = Input::get('comment');
        $comment->user_id = Auth::id();
    
        $comment->makeChildOf($parent);
    }
    

    The Relations, Root and Leaf scopes, Accessing the ancestry/descendancy chain section of the docs have several examples on how to then retrieve the comments for comments.

    Edit

    It looks like the Comment model in the package already extends the Baum\Node so you don't need to do that. In order to use this package, it looks like you need to use his Comment model. I am sure you could use his model as a base and roll your own.

    You could do something like this. You would have to set up a route.

    {!! Form::open(['route' => ['comment', $comment]]) !!}
    {!! Form::close() !!}

    0 讨论(0)
提交回复
热议问题