Recursion 如何在Laravel 4中实现递归搜索?
我正在尝试创建一个posts-comments关系表并显示结果 我认为我可以正确地创建所有关系,但我缺少视图中正确显示它们的递归函数。这是我的密码: 我的职位表是:Recursion 如何在Laravel 4中实现递归搜索?,recursion,laravel,Recursion,Laravel,我正在尝试创建一个posts-comments关系表并显示结果 我认为我可以正确地创建所有关系,但我缺少视图中正确显示它们的递归函数。这是我的密码: 我的职位表是: id | subject | content | id | content | post_id | parent_id 我的意见表是: id | subject | content | id | content | post_id | parent_id 如您所见,我将注释表与带有“post_id”的posts表
id | subject | content |
id | content | post_id | parent_id
我的意见表是:
id | subject | content |
id | content | post_id | parent_id
如您所见,我将注释表与带有“post_id”的posts表相关联,并将注释表自身与de“parent_id”相关联,以表示其他注释的子注释
现在,这些是我的模型。就我的职位而言:
<?php
class Post extends Eloquent{
public function comments(){//Me captura el primer bloque de replies
return $this->hasMany('Comment')->where('parent_id',0);
}
}
?>
我的评论模式是:
<?php
class Comment extends Eloquent{
public function posts(){
return $this->belongsTo('Post');
}
public function nestedComment(){
return $this->hasMany('Comment','parent_id');
}
}
?>
我认为这些关系应该足以创建视图(我不知道如何使用递归函数构建视图),到目前为止,我有以下几点:
@if($posts)
@foreach($posts as $post) {{-- for posts --}}
<strong>{{$post->subject}}</strong><br />
@foreach($post->comments as $comment) {{-- for 1st reply --}}
<p id="reply1">{{$comment->content}}</p>
@if($comment->nestedComment->count() >0) {{-- for reply reply --}}
@foreach($comment->nestedComment as $nestedComment)
<p id="reply2">{{$nestedComment->content}}</p>
@endforeach
@endif
@endforeach
@endforeach
@else
@endif
@if($posts)
@foreach($posts as$post){{--posts--}
{{$post->subject}
@foreach($post->comments as$comment){{{--1次回复--}
{{$comment->content}
@如果($comment->nestedComment->count()>0){{--for reply-reply--}
@foreach($comment->nestedComment作为$nestedComment)
{{$nestedComment->content}
@endforeach
@恩迪夫
@endforeach
@endforeach
@否则
@恩迪夫
当然,有了这种类型的视图,我可以只显示回复的第一个回复,但不能再深入了
你知道我如何构建这个视图并显示回复的深度吗
哥斯达黎加提前向您表示感谢 这就是我要尝试的:
function display_comment($comment, $indent) {
echo $indent, $comment, PHP_EOL;
if ( $comment->nestedComment )
display_comment($comment->nestedComment, $indent + INDENT);
}
但是我还没能实现它