【问题标题】:Laravel 5: how to do multi threaded commentsLaravel 5:如何进行多线程评论
【发布时间】:2015-10-13 05:10:05
【问题描述】:

我正在使用Laravel Commentable 包,它使用嵌套集模式和Baum

我已设法允许用户在帖子上创建 cmets,但它们没有线程化,每条评论在数据库中都将 depth 设置为零。

所以我想知道如何制作像 reddit 这样的多线程 cmets?

这是我的桌子

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

我的模型(评论和用户)

class Comment extends Model
{
    use Commentable;

    public function commentable()
    {
        return $this->morphTo();
    }

    public function user() {
        return $this->belongsTo('App\User');
    }

    public function posts() {
        return $this->belongsTo('App\Post');
    }
}

class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

    public function posts() {
        return $this->hasMany('App\Post');
    }

    public function comments() {
        return $this->hasMany('App\Comment');
    }
}

这就是我在 PostsController 中提交 cmets 的方式

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

这就是风景

    <div class="post-comments">

    {!! Form::open(['route' => ['comment', $post]]) !!}
        <div class="form-group">
            <label for="comment">Your Comment</label>
            <textarea name="comment" class="form-control" rows="3"></textarea>
        </div>
        <button type="submit" class="btn btn-default">Send</button>
    {!! Form::close() !!}

    <div class="comments-nav">
        <ul class="nav nav-pills">
            <li role="presentation" class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
                    there are {{ count($comments) }} comments <span class="caret"></span>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="#">Best</a></li>
                    <li><a href="#">Hot</a></li>
                </ul>
            </li>
        </ul>
    </div>

    <div class="row">

        <div class="media">
            <!-- first comment -->
            @foreach($comments as $comment)
            <div class="media-heading">
                <button class="btn btn-default btn-xs" type="button" data-toggle="collapse" data-target="#{{ $comment->id }}" aria-expanded="false" aria-controls="collapseExample"><span class="glyphicon glyphicon-minus" aria-hidden="true"></span></button> <span class="label label-info">12314</span> {{ $comment->user->name }} 12 hours ago
            </div>

            <div class="panel-collapse collapse in" id="{{ $comment->id }}">

                <div class="media-left">
                    <div class="vote-wrap">
                        <div class="vote up">
                            <i class="glyphicon glyphicon-menu-up"></i>
                        </div>
                        <div class="vote inactive">
                            <i class="glyphicon glyphicon-menu-down"></i>
                        </div>
                    </div>
                    <!-- vote-wrap -->
                </div>
                <!-- media-left -->


                <div class="media-body">
                    <p>{{ $comment->body }}</p>
                    <div class="comment-meta">
                        <span><a href="#">delete</a></span>
                        <span><a href="#">report</a></span>
                        <span><a href="#">hide</a></span>
          <span>
                    <a class="" role="button" data-toggle="collapse" href="#replyCommentT" aria-expanded="false" aria-controls="collapseExample">reply</a>
                  </span>
                        <div class="collapse" id="replyCommentT">
                            <form>
                                <div class="form-group">
                                    <label for="comment">Your Comment</label>
                                    <textarea name="comment" class="form-control" rows="3"></textarea>
                                </div>
                                <button type="submit" class="btn btn-default">Send</button>
                            </form>
                        </div>
                    </div>
                    <!-- comment-meta -->

                </div>
            </div>
            <!-- comments -->
            @endforeach
        </div>
        <!-- first comment -->

    </div>

</div>
<!-- post-comments -->

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    我没用过Laravel Commentable 包,但是docs 看起来还不错。

    我相信您的Post 模型上需要use Commentable;,而不是Comment 模型。

    看起来您的 Comment 模型需要扩展 Baum\Node 而不是 Model

    那么你所拥有的应该可以工作。

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

    要对评论发表评论,您似乎做了这样的事情。我可能会做一个 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);
    }
    

    文档的RelationsRoot and Leaf scopesAccessing the ancestry/descendancy chain 部分有几个示例,说明如何为 cmets 检索 cmets。

    编辑

    看起来包中的Comment 模型已经扩展了 Baum\Node,所以你不需要这样做。为了使用这个包,看起来你需要使用他的Comment 模型。我相信您可以将他的模型用作基础并推出您自己的模型。

    你可以做这样的事情。您必须设置路线。

    <div class="collapse" id="replyCommentT">    
    
        {!! Form::open(['route' => ['comment', $comment]]) !!}
    
            <input type="hidden" name="parent_id" value="{{ $comment->id }}"/>
    
            <div class="form-group">
    
                <label for="comment">Your Comment</label>
    
                <textarea name="comment" class="form-control" rows="3"></textarea>
    
            </div>
    
            <button type="submit" class="btn btn-default">Send</button>
    
        {!! Form::close() !!}
    
    </div>
    

    【讨论】:

    • 我使用的包没有提到扩展Baum\Node 你确定我应该这样做吗?我已经将Commentable 导入Post 模型。关于parent_id 的一个快速问题 - 我应该创建一个隐藏输入,并将$comment-&gt;id 作为值吗?
    • @Halnex A 在github.com/RomainLanz/laravel-commentable 的底部,他链接到github.com/etrepat/baum#model-configuration,它在那里谈论它。也试试看它是否有效。
    • @Halnex 显然Comment 模型已经扩展了 Node.js。 github.com/RomainLanz/laravel-commentable/blob/2.0/src/…
    • 所以我不需要创建自己的Comment 模型?我刚刚扩展了Baum 并且评论仍然有效。我仍然对如何从视图中检索 parent_id 感到困惑。我应该使用 $comment-&gt;id 作为值的隐藏输入吗?
    • @Halnex 你使用他们的。它不适用于您的,因为 Commentable 特征返回他们的类,而不是您的自定义类。
    猜你喜欢
    • 2017-07-09
    • 2016-12-02
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2016-12-02
    • 2016-07-06
    相关资源
    最近更新 更多