[英]how to get 1 recent comments of a post on laravel
so on my index blade im trying to show one lastest for the post and it seems im getting error please any help i will really appreciate the error im getting所以在我的索引刀片上,我试图显示一个最新的帖子,似乎我遇到了错误,请提供任何帮助,我将非常感谢我收到的错误
Error Call to a member function last() on string (View:在字符串上调用成员函数 last() 时出错(查看:
@foreach ( $post->comments as $comment )
{{ $comment->body->last() }}
@endforeach
你可以使用$post->comments->latest()->first();
你可以像下面这样使用
@foreach ($post->comments->latest()->first() as $comment)
If you have already defined the relationship in post model like this:如果您已经在 post 模型中定义了这样的关系:
public function comments()
{
return hasMany(Post::class)
}
Just retrive data like this:
$post = Post::with('comments')->latest()->first();
Now in view file just use:现在在视图文件中只需使用:
$post->comments;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.