Laravel中的timestampTz字段 [英] timestampTz fields in Laravel
问题描述
Laravel 5.4在迁移中支持Postgres TIMESTAMP WITH TIME ZONE
字段类型:
Laravel 5.4 supports the Postgres TIMESTAMP WITH TIME ZONE
field type in migrations:
$table->timestampTz('scheduled_for');
可以设置
Laravel将日期字段(DATE
,DATETIME
,TIMESTAMP
)转换为Carbon对象(默认情况下,将created_at
和updated_at
TIMESTAMP
字段也是如此),但是将scheduled_for
放入$dates
字段会导致时区感知版本发生错误:
Laravel can be set up to convert date fields (DATE
, DATETIME
, TIMESTAMP
) into Carbon objects (and does so by default for the created_at
and updated_at
TIMESTAMP
fields), but putting scheduled_for
into the $dates
field causes an error with the timezone-aware version:
InvalidArgumentException with message 'Trailing data'
查看数据库并进行修改,该字段的值似乎类似于2017-06-19 19:19:19-04
.是否有一种从这些字段类型之一中获取Carbon对象的本地方法?还是我使用访问器卡住了?
Looking in the database and tinker, the field's value appears to be something like 2017-06-19 19:19:19-04
. Is there a native way to get a Carbon object out of one of these field types? Or am I stuck using an accessor?
推荐答案
重提此问题,希望能得到接受的有用答案.
Resurrecting this question, hopefully with a helpful answer that gets accepted.
Laravel假定为Y-m-d H:i:s
数据库时间戳格式.如果您使用的是Postgres timestampz
列,则明显不同.您需要告诉Eloquent如何让Carbon解析该格式.
Laravel assumes a Y-m-d H:i:s
database timestamp format. If you're using a Postgres timestampz
column, that's obviously different. You need to tell Eloquent how to get Carbon to parse that format.
只需在模型上定义$dateFormat
属性,如下所示:
Simply define the $dateFormat
property on your model like so:
Class MyModel extends Eloquent {
protected $dateFormat = 'Y-m-d H:i:sO';
}
应归功于信用:我在 GitHub问题中找到了这种解决方案
这篇关于Laravel中的timestampTz字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!