碳获取当前日期时变量为空,即使受保护$dates Laravel 5.6



我在 laravel 5.6 中遇到了碳问题。 从数据库返回数据时,如果时间戳字段具有空值,则 Carbon 返回当前日期,即使我有protected $dates.

数据库中的published_at为空。

型:

protected $dates = ['published_at'];

叶片:

value="{{ old('published_at', $post->published_at)}}"

我已经尝试了关于碳的建议,如果变量为空,则获取当前日期,但有什么建议吗?

您可以在访问器的帮助下解决问题。因此,不要将published_at添加到$dates属性中,而只需定义此访问器:

public function getPublishedAtAttribute($published_at)
{
return is_null($published_at) ? null : Carbon::parse($published_at);
}

最新更新