laravel - 如何在 laravel 中显示评论和回复评论
问题描述
我不知道如何在帖子页面中显示评论和回复评论,我的评论表包含 id、sender_id、replyer_id、reply_id、comment_text。
CommentController 返回评论对象。我的问题是在 Post.blade.php 如何编写 foreach 循环或循环。请帮我。
解决方案
控制器代码必须与此类似
public function index()
{
$comments = Comment::with(['sender', 'other-relation'])->get();
return view('comments.index', compact('comments'));
}
在刀片代码中必须与此类似
<ul>
@foreach($comments as $comment)
<li>{{ $comment->comment_text }}</li>
@if ($comment->sender) // or other relation
<a> {{$comment->sender->name}}<a> // relation name and column name must be fix yourself
@endif
@endforeach
</ul>
推荐阅读
- python - 从目录估计并创建特定大小的 zip 文件
- elasticsearch - 如何将日志从 serilog 文件发送到 elasticsearch
- oracle - 无法在 Mac 的 docker 中的 Oracle 11g 中假脱机,文件路径或权限问题?(SP2-0606:无法创建 SPOOL 文件)
- gitlab-ci - 未从 Gitlab CI 管道中的测试/资源中选择 Quarkus application.properties
- reactjs - 如何使用 React(使用 material-ui 和 react-mathjax-node)在 MathJax 上包装文本
- javascript - Angular 11 Electron IPC 通信“发送”未定义
- php - WooCommerce 存款:仅保留存款的特定运输方式
- child-process - Netlify 在构建过程中有不同的 pid
- c# - 等待任务忽略超时
- javascript - $(...).modal 不是函数 jquery.min.js