【发布时间】:2018-06-26 00:59:58
【问题描述】:
如何使用嵌套的 foreach 在 laravel 中显示帖子和 cmets?
这是我的路线:
Route::get('/showpost', function () {
$showpost = Posts::all();
$comment = Comment::all();
return view('showpost', compact('showpost', 'comment'));
});
这是我的观点:
<h1>Posts :</h1>
@foreach($showpost as $showpost)
<h1>{{ $showpost->Title }}</h1>
<h5>{{ $showpost->Content }}</h5>
{{ Form::open(array('url'=>'showpost','method'=>'post')) }}
<h5>{{ Form::label('Comment : ') }} {{ Form::text('Comment') }}</h5>
{{ Form::hidden('invisible', $showpost->ID) }}
{{ Form::submit('Send Comment') }}
{{ Form::close() }}
<hr style='margin-top: 10%'>
@endforeach
<hr>
<h1>Comments :</h1>
@foreach($comment as $comment)
{{ $comment->Comment }} -> {{ $comment->PostID }} <br>
@endforeach
这里显示帖子,下面也显示一个文本框,我们可以添加评论 我想向 cmets 展示每个帖子下的帖子
现在我不能使用嵌套的 foreach!!!
【问题讨论】:
-
你的帖子和cmets之间的模型中添加了雄辩的关系吗?
-
我建议您看看这个Laracast 系列,它将向您展示如何实现这样的关系。
-
在阅读文档之前,您是否在数据库迁移中添加了外键?