Laravel 5:如何做多线程注释 [英] Laravel 5: how to do multi threaded comments
问题描述
我正在使用 Laravel Commentable 软件包,该软件包将嵌套集模式与
I am using Laravel Commentable package which uses Nested Sets pattern with Baum.
我设法允许用户对帖子发表评论,但是他们没有主题,数据库中每个评论的depth
都设置为零.
I have managed to allow users to make comments on posts but they're not threaded, each comment has depth
set to zero in the database.
所以我想知道如何进行诸如reddit之类的多线程注释?
So I'm wondering how does one go about making multi threaded comments like reddit for example?
这些是我的桌子
users: id, name, email...
posts: id, user_id, subreddit_id...
comments: id, body, parent_id, lft, rgt, depth, commentable_id, commentable_type, user_id
我的模型(评论和用户)
My Models (Comment and User)
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
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 -->
推荐答案
我没有使用 Laravel可注释包,但文档看起来还不错.
I haven't used Laravel Commentable package, but the docs look pretty good.
我相信您需要在Post
模型上使用use Commentable;
而不是在Comment
模型上.
I believe you need use Commentable;
on your Post
model and not your Comment
model.
您的Comment
模型似乎需要扩展Baum\Node
而不是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);
要对评论发表评论,看起来您在做这样的事情.我可能会做一个CommentController.
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.
修改
它看起来像评论模型软件包中的已经扩展了Baum \ Node,所以您不需要这样做.为了使用此软件包,您似乎需要使用他的
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.
<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>
这篇关于Laravel 5:如何做多线程注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!