找不到文本月份,尾随数据 Carbon - laravel

A textual month could not be found ,Trailing data Carbon - laravel

我尝试了不同的方法,但我没有得到正确的时间格式。

$news->created_at = Carbon::parse($news->created_at)->format('M, d, Y');

$news->created_at = date('d M Y',strtotime($news->created_at))

$news->created_at = date('d M Y',$news->created_at)

$news->created_at = Carbon::createFromFormat("d M Y",strtotime($news->created_at));

$news->created_at = $news->created_at->format('M, d, Y');

错误是,

Unexpected data found
The separation symbol could not be found
InvalidArgumentException

Carbon.php: 910

dd($新闻->created_at);

Carbon @1550035143 {#361 ▼
  date: 2019-02-13 05:19:03.0 UTC (+00:00)
}

您的 $news->created_at 字段中已经有一个 Carbon 实例,因为 Eloquent 模型考虑了来自 Carbon 的 created_at and updated_at columns as timestamps by default and automatically convert them to Carbon instances. So you just need to use the format 方法:

$news->created_at->format('d M Y');

但是,当您尝试在模型实例上将字符串重新分配为 created_at 的值时,它会与 Laravel 试图将分配给日期的任何值转换的内部更改器冲突从 Carbon 实例到字符串的字段。

您可以在您的 News 模型中设置 public $timestamps = false;,然后在处理模型时间戳时在整个应用程序中使用字符串,但这看起来更像是一种 hack 而不是解决方案,因为您会给出了解 Carbon 提供的所有好处。

您也可以通过在序列化时处理时间戳来做到这一点,像这样:

return collect($news->makeHidden(['created_at']))->merge([
    'created_at' => $news->created_at->format('d M Y')
]);

以上代码将从序列化过程中隐藏传递给 makeHidden 的列。然后您可以将隐藏列的格式化值合并到您的响应中。