简体   繁体   中英

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:

 @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:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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