线程注释、php、laravel
这是我的表格新主题\u评论:线程注释、php、laravel,php,laravel,Php,Laravel,这是我的表格新主题\u评论: id parent_id id_theme user text upVotes downVotes 这是我的控制器: class Comments extends Controller { public function GenerateComments($id){ $Comments = NewTheme_Comment::where('id_theme', $id)->paginate(
id
parent_id
id_theme
user
text
upVotes
downVotes
这是我的控制器:
class Comments extends Controller {
public function GenerateComments($id){
$Comments = NewTheme_Comment::where('id_theme', $id)->paginate(5);
return view('comments', ['Comments'=>$Comments]);
}
@foreach ($Comments as $Comment) {{-- Comments threads --}}
<div class="media">
<div class="media-left">
</div>
<div class="media-body">
<p class="media-heading">{{ $Comment->text }}</p>
<p class="media-heading">{{ $Comment->user }} / {{ $Comment->created_at }} </p>
</div>
</div>
@endforeach {{-- Comments threads --}}
0
1
2
3
2
1
1
0
1
0
Id是指向一般帖子的链接(每个帖子都有不同的评论部分),因此在用户单击时,用户会动态地重定向到相应的评论部分(“评论视图”)
因此,我有一个数组Comments,其中填充了表NewTheme_Comment中的值,我的问题在于如何使用这些值创建一个线程注释部分
在我看来也是如此(问题是,这使得注释部分与每个家长id都等于0一样,这不是我想要的:
class Comments extends Controller {
public function GenerateComments($id){
$Comments = NewTheme_Comment::where('id_theme', $id)->paginate(5);
return view('comments', ['Comments'=>$Comments]);
}
@foreach ($Comments as $Comment) {{-- Comments threads --}}
<div class="media">
<div class="media-left">
</div>
<div class="media-body">
<p class="media-heading">{{ $Comment->text }}</p>
<p class="media-heading">{{ $Comment->user }} / {{ $Comment->created_at }} </p>
</div>
</div>
@endforeach {{-- Comments threads --}}
0
1
2
3
2
1
1
0
1
0
我的想法是发表如下评论(使用家长id):
但是我找不到正确的方法在逻辑上做这件事,因此最终看起来像一个简单的线程注释部分,与reddit使用的相同(对于我的小web应用程序)
如果我的方法不好,我将非常感谢其他更好的方法来解决这个问题。列出每个没有父id的注释,然后在加载注释时检查这些注释是否有childs注释,并列出带有缩进的注释。列出每个没有父id的注释,然后在加载c注释检查这些注释中是否有child注释,并列出带有缩进的注释。这是一个多步骤解决方案。您应该更喜欢使用包,因为线程注释是嵌套集的一种形式 另一种方法是,如果您想继续使用当前的方法,将在
Comment
模型中使用child
函数,如下所示
public function children(){
return $this->hasMany('App\Models\Comment', 'parent_id'); //Change as per your application architecture
}
然后,在视图中,可以通过递归调用包含
//print the details of $Comment
@foreach($Comment->children as $childComment)
//call this partial view again for $childComment & print the details
@endforeach
这是一个多步骤的解决方案。您应该更喜欢使用包,因为线程注释是嵌套集的一种形式 另一种方法是,如果您想继续使用当前的方法,将在
Comment
模型中使用child
函数,如下所示
public function children(){
return $this->hasMany('App\Models\Comment', 'parent_id'); //Change as per your application architecture
}
然后,在视图中,可以通过递归调用包含
//print the details of $Comment
@foreach($Comment->children as $childComment)
//call this partial view again for $childComment & print the details
@endforeach
您可以按父级id对它们进行分组,这样它们的结构就正确了,这样您就可以嵌套输出它们。如何按父级id对它们进行分组,如(0,1,1,1,2,2,2/0,1,1,2,2,2)注释树的第一个分支和第二个分支(2个单线程注释)?您可以按父级id对它们进行分组,这样它们的结构就会正确,这样您就可以嵌套输出它们。如何按父级id对它们进行分组,如(0,1,1,1,2,2,2,2/0,1,1,2,2,2)注释树的第一个分支和第二个分支(2个单线程注释)?