繁体   English   中英

如何在 Laravel 上获得 1 个帖子的最近评论

[英]how to get 1 recent comments of a post on laravel

所以在我的索引刀片上,我试图显示一个最新的帖子,似乎我遇到了错误,请提供任何帮助,我将非常感谢我收到的错误

在字符串上调用成员函数 last() 时出错(查看:

 @foreach ( $post->comments as $comment )
      {{ $comment->body->last() }}
   @endforeach

你可以使用$post->comments->latest()->first();

你可以像下面这样使用

@foreach ($post->comments->latest()->first() as $comment)

如果您已经在 post 模型中定义了这样的关系:

public function comments()
   {
    return hasMany(Post::class)
   }
Just retrive data like this:
$post = Post::with('comments')->latest()->first();

现在在视图文件中只需使用:

$post->comments;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM