我正在接收对成员函数 diffForHumans 字符串错误的调用

I am receiving call to a member function diffForHumans string error

我的方法是这样的:

public function show(Game $game)
{
    $discussion = DB::table('chatter_post')->whereId($game->chatter_post_id)->first();

    $chatter_post = DB::table('chatter_post')->where('chatter_discussion_id', $discussion->chatter_discussion_id)
                                             ->whereNotIn('id', [$game->chatter_post_id])->get();

                                             dd($chatter_post);
    return view('games.games', ['game' => $game, 'discussions' => $chatter_post]);
}

在dd($chatter_post)中dump和die时能看到所有数据:

0 => {#861 ▼
  +"id": 20
  +"chatter_discussion_id": 25
  +"user_id": 1
  +"body": "<p>Everything will be placed here</p>"
  +"created_at": "2018-11-28 08:10:39"
  +"updated_at": "2018-11-28 08:10:39"
  +"markdown": 0
  +"locked": 0
  +"deleted_at": null
}

但是在前端,当我尝试使用 diffForHumans 时,我看到了这个错误:

Call to a member function diffForHumans() on string 

我的前端是这样的:

@foreach($discussions as $discussion)       
    <div class="flex">
        <div class="avatar"></div>
        <blockquote class="main_content">
            <p class="author_created">Author Name {{$discussion->created_at->diffForHumans()}} </p>
            <div>{!!$discussion->body!!}</div>
        </blockquote>
    </div>
@endforeach

为什么调用 getDIffForHumans 时会出现错误?

您的日期不是 Carbon 的一个实例,因此解析它然后执行 diff,如:

{{ \Carbon\Carbon::parse($discussion->created_at)->diffForHumans() }}