【发布时间】:2015-04-30 05:02:41
【问题描述】:
我正在尝试在 Laravel 中使用 Blade 生成嵌套的 cmets。似乎我必须制作一个刀片模板,该模板为每个评论预配置了无限的嵌套 cmets,它是子 cmets。但我希望 cmets 自动生成。
这是我的评论模型:
class Comment extends Model {
protected $table = 'comments';
public function user()
{
return $this->hasOne('App\Models\User', 'id', 'user_id');
}
public function post()
{
return $this->hasOne('App\Models\Post', 'id', 'post_id');
}
public function children()
{
return $this->hasMany('App\Models\Comment', 'parent_id', 'id');
}
}
在我看来我正在做
@foreach($comments as $comment)
<!-- Comment markup -->
@if($comment->children->count() > 0)
@foreach($comment->children as $child)
<!-- Child comment markup -->
@if($child->children->count() > 0) // I have to do this unlimited times
@foreach ....
@endforeach
@endif
@endif
@endforeach
我正在寻找一种方法来自动执行此操作,可能使用函数或其他东西。
【问题讨论】: