根据文档,Laravel会自动将created_at
和updated_at
(来自Eloquent模型)传递到新的Carbon实例中。
看起来,如果该值是默认值0000-00-00 00:00:00
,则输出以下内容:
所有-0001-11-30 06:12:32
值均为0000-00-00 00:00:00
。
字段设置为时间戳类型。
我现在正在使用以下内容(在模型中),但是在所有可能包含默认/未设置日期的Laravel模型中执行此操作会感到笨拙。
public function getCreatedAtAttribute($value) {
return $value == "0000-00-00 00:00:00" ? "0000-00-00 00:00:00" : $value;
}
答案 0 :(得分:2)
这发生在model.php
中的getAttributeValue方法中elseif (in_array($key, $this->getDates()))
{
if ($value) return $this->asDateTime($value);
}
因为它被传递给asDateTime方法。 这可以用
之类的东西来解决elseif (in_array($key, $this->getDates()))
{
if ($value && $value !== '0000-00-00 00:00:00') return $this->asDateTime($value);
}
这可能是拉取请求的问题吗?
答案 1 :(得分:2)
似乎没有干净的方法来做到这一点。所以我选择了:
public function getCreatedAtAttribute($value) {
return $value == "0000-00-00 00:00:00" ? "0000-00-00 00:00:00" : $value;
}