gpt4 book ai didi

php - 无法显示外键值

转载 作者:行者123 更新时间:2023-11-29 05:02:09 25 4
gpt4 key购买 nike

我正在尝试创建一个帖子和评论部分,我设法在一个页面中显示所有帖子,现在我试图在每个帖子下显示评论。在我的模型中,我连接了两个表,在我的函数中,当我使用 dd(); 时,我可以查看帖子和评论这是我的结果:

dd();

我不确定我是否在我的 View 中正确调用了评论部分,因为我收到错误:

Property [activity_post_comments] does not exist on this collection instance. (View: 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);
}

查看:

@if(count($activityposts) > 0)
@foreach($activityposts as $activitypost)
<div class="panel panel-default">
<!--
<div class="panel-heading"><a href="#" class="pull-right">View all</a>
<h4>Bootply Editor &amp; Code Library</h4></div>
-->
<div class="panel-body">
<p>
<img src="../storage/user_image/{{ $activitypost->user->user_image }}" width="28px" height="28px">
<b>{{ $activitypost->user->firstname }} {{ $activitypost->user->lastname }}</b>
<span class="pull-right">{{ $activitypost->created_at->format('d-m-Y') }}</span>
</p>
<!--
<div class="clearfix"></div>
<hr>
-->
{{ $activitypost->status_update }}
<hr>
<div class="well">
{{ $activitypost->activity_post_comments->activity_post_comments }};
</div>
<hr>
<form class="form-horizontal" role="form" method="POST" enctype="multipart/form-data" action="{{ action('PagesController@storecomments', $activitypost->id) }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('activity_post_comments') ? ' has-error' : '' }}">
<!-- <label for="activity_post_comments" class="col-md-4 control-label">First Name</label> -->
<div class="col-md-12">
<input id="activity_post_comments" type="text" class="form-control" name="activity_post_comments" required autofocus>
@if ($errors->has('activity_post_comments'))
<span class="help-block">
<strong>{{ $errors->first('activity_post_comments') }}</strong>
</span>
@endif
</div>
</div>
<button type="submit" class="btn btn-primary">Post</button>
</form>
</div>
</div>
@endforeach
{{$activityposts->links()}}
@else
<p>No new activity from users</p>
@endif

这是我不确定的地方:

{{ $activitypost->activity_post_comment->activity_post_comments }};

最佳答案

由于 activity_post_comment 是一个集合,您需要遍历它以获取单个实例。

@foreach ($activitypost->activity_post_comment as $comment)
<p>{{ $comment->activity_post_comments }}</p>
@endforeach

关于php - 无法显示外键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56371171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com