Я пытаюсь создать разделы «Пост» и «Комментарии», мне удалось показать все сообщения на одной странице, теперь я пытаюсь показать комментарии под каждым сообщением. В моей модели я соединил обе таблицы, и в моей функции я могу просматривать пост и комментарии, когда использую dd();
, это мой результат:
Я не уверен, правильно ли я называю часть комментария в своих представлениях, потому что получаю сообщение об ошибке:
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 & 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
как указывал linktoahref, то, в чем вы не уверены, правильно. Отношение между $activitypost и $activity_post_comment равно 1-n, или $activitypost будет иметь коллекцию $activity_post_comment, поэтому при работе с коллекцией вы можете использовать цикл for или другой удобный вспомогательный метод. цикл for может быть
@foreach ($activitypost->activity_post_comment as $comment)
<p>{{ $comment->activity_post_comments }}</p>
@endforeach
или если вам просто нужен массив activity_post_comments
$activitypost->activity_post_comment->pluck('activity_post_comments')