簡體   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