Php 无法显示外键值
我试图创建一个帖子和评论部分,我设法在一个页面中显示所有帖子,现在我试图在每个帖子下显示评论。在我的模型中,我连接了两个表,在我的函数中,当我使用Php 无法显示外键值,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我试图创建一个帖子和评论部分,我设法在一个页面中显示所有帖子,现在我试图在每个帖子下显示评论。在我的模型中,我连接了两个表,在我的函数中,当我使用dd()时,我能够查看帖子和评论这是我的结果: 我不确定我是否在视图中正确调用了注释部分,因为我遇到了错误: 此集合实例上不存在属性[activity\u post\u comments]。 (视图:C:\xampp\xampp\htdocs\rsapp\resources\views\pages\timeline.blade.php) 我的职能:
dd()时,我能够查看帖子和评论代码>这是我的结果:
我不确定我是否在视图中正确调用了注释部分,因为我遇到了错误:
此集合实例上不存在属性[activity\u post\u comments]。
(视图:C:\xampp\xampp\htdocs\rsapp\resources\views\pages\timeline.blade.php)
我的职能:
public function timeline()
{
$activityposts = ActivityPost::with('activity_post_comment')
->orderBy('created_at','desc')
->paginate(100);
$posts = Post::orderBy('created_at','desc')->paginate(100);
// dd($activityposts);
return view('pages.timeline')
->with('posts', $posts)
->with('activityposts', $activityposts);
}
视图:
由于activity\u post\u comment
将是一个集合,因此需要循环使用相同的集合来获取单个实例
@foreach ($activitypost->activity_post_comment as $comment)
<p>{{ $comment->activity_post_comments }}</p>
@endforeach
@foreach($activitypost->activity\u post\u comment作为$comment)
{{$comment->activity\u post\u comments}
@endforeach
正如linktoahref所指出的,你不确定的事情是正确的。
$activitypost和$activity\u post\u comment之间的关系为1-n,
或者$activitypost将收集$activity\u post\u评论,
因此,在使用集合时,可以使用for循环或其他方便的辅助方法。
for循环可能是
@foreach ($activitypost->activity_post_comment as $comment)
<p>{{ $comment->activity_post_comments }}</p>
@endforeach
@foreach ($activitypost->activity_post_comment as $comment)
<p>{{ $comment->activity_post_comments }}</p>
@endforeach
@foreach ($activitypost->activity_post_comment as $comment)
<p>{{ $comment->activity_post_comments }}</p>
@endforeach
$activitypost->activity_post_comment->pluck('activity_post_comments')