在 laravel 中创建嵌套评论
Creating nested comments in laravel
我有以下博客控件
public function show($slug)
{
$post = Blog::where('slugs', '=', $slug)->first();
$vars['pageTitle'] = Config::get('site.pageTitle') . $post['title'];
// The breadcrumbs... needs to be repopulated every page
$vars['breadCrumbs'] = [[
'url' => action('SimpleController@index'),
'title' => 'CovertDEV'
],[
'url' => action('BlogController@index'),
'title' => 'Blog'
],[
'url' => route('blog_post', ['slug' => $slug]),
'title' => $post['title']
]];
$vars['blog'] = $post;
$vars['comments'] = $post->Blog_comments->groupBy('comment_id');
return view('blog', $vars);
}
还有下面吓人的观点求评论
@php
function display_comments($main, $depth = "\t")
{
foreach($main[0] as $mcomment)
{
@endphp
<div class="media">
<img class="m-3 avatar-sm rounded-1 border border-thick-1 shadow" src="../public/css/images/avatar-placeholder.png" alt="<?=$mcomment->firstname;?> <?=$mcomment->lastname;?>'s Avatar">
<div class="media-body">
<h6 class="mt-0 p-1 lead m-0 border-bottom"><?=$mcomment->firstname;?> <?=$mcomment->lastname;?></h6>
<div class="p-2">
<p><?=$mcomment->comment;?></p>
</div>
@php
if(isset($main[$mcomment->id]))
{
display_child($main, $main[$mcomment->id], $depth);
}
@endphp
</div>
</div>
@php
}
}
function display_child($main, $comment, $depth)
{
foreach($comment as $ccomment)
{
@endphp
<div class="media">
<img class="m-3 avatar-sm rounded-1 border border-thick-1 shadow" src="../public/css/images/avatar-placeholder.png" alt="<?=$ccomment->firstname;?> <?=$ccomment->lastname;?>'s Avatar">
<div class="media-body">
<h6 class="mt-0 p-1 lead m-0 border-bottom"><?=$ccomment->firstname;?> <?=$ccomment->lastname;?></h6>
<div class="p-2">
<p><?=$ccomment->comment;?></p>
</div>
@php
if(isset($main[$ccomment->id]))
{
display_child($main, $main[$ccomment->id], $depth."\t");
}
@endphp
</div>
</div>
@php
}
}
display_comments($comments);
@endphp
这太丑陋了。它生成了我想要的样子,但这太丑了,丑得可怕。
This is an image of the created nested comments
在 blade 模板中优雅地完成这项工作的最佳方法是什么?我尝试根据 docs 扩展 blade 模板,但这对我来说并没有取得成果。我根本不知道该怎么做。
有了这个,我不能调用资产或类似的东西......那些已经超出了范围。
我终于找到了令我满意的解决方案。如果大家有更好的解决方案,欢迎提出。
我的博客控制器是一样的,唯一改变的是我的视图。在我的博客视图中(我以用户身份看到这些评论):
blog.blade.php
@foreach($comments[0] as $comment)
<div class="media comment">
@if(!is_null($comment->users['avatar']))
<img class="m-3 avatar-sm rounded-1 border border-thick-1 shadow" src="{{ asset( Config::get('site.asset_folder') . 'users/images/' . $comment->users['avatar']) }}" alt="{{ $comment->users['firstname'] }} {{ $comment->users['lastname'] }}'s Avatar">
@else
<img class="m-3 avatar-sm rounded-1 border border-thick-1 shadow" src="{{ asset( Config::get('site.asset_folder') . 'css/images/avatar-placeholder.png') }}" alt="{{ $comment->users['firstname'] }} {{ $comment->users['lastname'] }}'s Avatar">
@endif
<div class="media-body">
<h6 class="mt-0 p-1 lead m-0 border-bottom">{{ $comment->users['firstname'] }} {{ $comment->users['lastname'] }}</h6>
<div class="p-2">
<p>{{ $comment->comment }}</p>
</div>
@if(isset($comments[$comment->id]))
@include('block.comments', ['comments' => $comments, 'parent_id' => $comment->id, 'depth' => 0])
@endif
</div>
</div>
@endforeach
然后,我的block/comments.blade.php
@foreach($comments[$parent_id] as $comment)
@if($depth >= Config::get('site.max_nested'))
</div>
</div>
@endif
<div class="media comment">
@if(!is_null($comment->users['avatar']))
<img class="m-3 avatar-sm rounded-1 border border-thick-1 shadow" src="{{ asset( Config::get('site.asset_folder') . 'users/images/' . $comment->users['avatar']) }}" alt="{{ $comment->users['firstname'] }} {{ $comment->users['lastname'] }}'s Avatar">
@else
<img class="m-3 avatar-sm rounded-1 border border-thick-1 shadow" src="{{ asset( Config::get('site.asset_folder') . 'css/images/avatar-placeholder.png') }}" alt="{{ $comment->users['firstname'] }} {{ $comment->users['lastname'] }}'s Avatar">
@endif
<div class="media-body">
<h6 class="mt-0 p-1 lead m-0 border-bottom">{{ $comment->users['firstname'] }} {{ $comment->users['lastname'] }}</h6>
<div class="p-2">
<p>{{ $comment->comment }}</p>
</div>
@if(isset($comments[$comment->id]))
@include('block.comments', ['comments' => $comments, 'parent_id' => $comment->id, 'depth' => $depth + 1])
@endif
@if($depth < Config::get('site.max_nested'))
</div>
</div>
@endif
@endforeach
然后我在配置文件夹中有一个配置文件site
,内容如下
return [
// We define the max depth for the nested comments
// You could obviously define this value in database and be retrieving it from there
'max_nested' => 3,
];
我的 $comments
数组的方式是 set-up,父评论总是在 $comments[0]
而子评论在 $comments[$parent_id]
我认为这相当简单但功能有效!
我有以下博客控件
public function show($slug)
{
$post = Blog::where('slugs', '=', $slug)->first();
$vars['pageTitle'] = Config::get('site.pageTitle') . $post['title'];
// The breadcrumbs... needs to be repopulated every page
$vars['breadCrumbs'] = [[
'url' => action('SimpleController@index'),
'title' => 'CovertDEV'
],[
'url' => action('BlogController@index'),
'title' => 'Blog'
],[
'url' => route('blog_post', ['slug' => $slug]),
'title' => $post['title']
]];
$vars['blog'] = $post;
$vars['comments'] = $post->Blog_comments->groupBy('comment_id');
return view('blog', $vars);
}
还有下面吓人的观点求评论
@php
function display_comments($main, $depth = "\t")
{
foreach($main[0] as $mcomment)
{
@endphp
<div class="media">
<img class="m-3 avatar-sm rounded-1 border border-thick-1 shadow" src="../public/css/images/avatar-placeholder.png" alt="<?=$mcomment->firstname;?> <?=$mcomment->lastname;?>'s Avatar">
<div class="media-body">
<h6 class="mt-0 p-1 lead m-0 border-bottom"><?=$mcomment->firstname;?> <?=$mcomment->lastname;?></h6>
<div class="p-2">
<p><?=$mcomment->comment;?></p>
</div>
@php
if(isset($main[$mcomment->id]))
{
display_child($main, $main[$mcomment->id], $depth);
}
@endphp
</div>
</div>
@php
}
}
function display_child($main, $comment, $depth)
{
foreach($comment as $ccomment)
{
@endphp
<div class="media">
<img class="m-3 avatar-sm rounded-1 border border-thick-1 shadow" src="../public/css/images/avatar-placeholder.png" alt="<?=$ccomment->firstname;?> <?=$ccomment->lastname;?>'s Avatar">
<div class="media-body">
<h6 class="mt-0 p-1 lead m-0 border-bottom"><?=$ccomment->firstname;?> <?=$ccomment->lastname;?></h6>
<div class="p-2">
<p><?=$ccomment->comment;?></p>
</div>
@php
if(isset($main[$ccomment->id]))
{
display_child($main, $main[$ccomment->id], $depth."\t");
}
@endphp
</div>
</div>
@php
}
}
display_comments($comments);
@endphp
这太丑陋了。它生成了我想要的样子,但这太丑了,丑得可怕。
This is an image of the created nested comments
在 blade 模板中优雅地完成这项工作的最佳方法是什么?我尝试根据 docs 扩展 blade 模板,但这对我来说并没有取得成果。我根本不知道该怎么做。
有了这个,我不能调用资产或类似的东西......那些已经超出了范围。
我终于找到了令我满意的解决方案。如果大家有更好的解决方案,欢迎提出。
我的博客控制器是一样的,唯一改变的是我的视图。在我的博客视图中(我以用户身份看到这些评论):
blog.blade.php
@foreach($comments[0] as $comment)
<div class="media comment">
@if(!is_null($comment->users['avatar']))
<img class="m-3 avatar-sm rounded-1 border border-thick-1 shadow" src="{{ asset( Config::get('site.asset_folder') . 'users/images/' . $comment->users['avatar']) }}" alt="{{ $comment->users['firstname'] }} {{ $comment->users['lastname'] }}'s Avatar">
@else
<img class="m-3 avatar-sm rounded-1 border border-thick-1 shadow" src="{{ asset( Config::get('site.asset_folder') . 'css/images/avatar-placeholder.png') }}" alt="{{ $comment->users['firstname'] }} {{ $comment->users['lastname'] }}'s Avatar">
@endif
<div class="media-body">
<h6 class="mt-0 p-1 lead m-0 border-bottom">{{ $comment->users['firstname'] }} {{ $comment->users['lastname'] }}</h6>
<div class="p-2">
<p>{{ $comment->comment }}</p>
</div>
@if(isset($comments[$comment->id]))
@include('block.comments', ['comments' => $comments, 'parent_id' => $comment->id, 'depth' => 0])
@endif
</div>
</div>
@endforeach
然后,我的block/comments.blade.php
@foreach($comments[$parent_id] as $comment)
@if($depth >= Config::get('site.max_nested'))
</div>
</div>
@endif
<div class="media comment">
@if(!is_null($comment->users['avatar']))
<img class="m-3 avatar-sm rounded-1 border border-thick-1 shadow" src="{{ asset( Config::get('site.asset_folder') . 'users/images/' . $comment->users['avatar']) }}" alt="{{ $comment->users['firstname'] }} {{ $comment->users['lastname'] }}'s Avatar">
@else
<img class="m-3 avatar-sm rounded-1 border border-thick-1 shadow" src="{{ asset( Config::get('site.asset_folder') . 'css/images/avatar-placeholder.png') }}" alt="{{ $comment->users['firstname'] }} {{ $comment->users['lastname'] }}'s Avatar">
@endif
<div class="media-body">
<h6 class="mt-0 p-1 lead m-0 border-bottom">{{ $comment->users['firstname'] }} {{ $comment->users['lastname'] }}</h6>
<div class="p-2">
<p>{{ $comment->comment }}</p>
</div>
@if(isset($comments[$comment->id]))
@include('block.comments', ['comments' => $comments, 'parent_id' => $comment->id, 'depth' => $depth + 1])
@endif
@if($depth < Config::get('site.max_nested'))
</div>
</div>
@endif
@endforeach
然后我在配置文件夹中有一个配置文件site
,内容如下
return [
// We define the max depth for the nested comments
// You could obviously define this value in database and be retrieving it from there
'max_nested' => 3,
];
我的 $comments
数组的方式是 set-up,父评论总是在 $comments[0]
而子评论在 $comments[$parent_id]
我认为这相当简单但功能有效!