我正在使用Laravel Commentable包,它使用嵌套集模式和Baum。
我设法允许用户对帖子发表评论,但他们没有线程,每条评论在数据库中都设置为depth
。
所以我想知道如何制作像reddit这样的多线程评论?
这些是我的表
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
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 -->
答案 0 :(得分:6)
我还没有使用Laravel Commentable包,但docs看起来还不错。
我认为您的use Commentable;
型号需要Post
而不是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);
}
文档的Relations,Root and Leaf scopes,Accessing the ancestry/descendancy chain部分有几个关于如何检索评论评论的示例。
修改强>
看起来包中的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>