【问题标题】:Laravel 4 Carbon trailing data errorLaravel 4 Carbon尾随数据错误
【发布时间】:2018-07-10 14:32:32
【问题描述】:

从我的数据库中检索时间戳字段并将其转换为 json 以进行响应时,我收到以下错误。

InvalidArgumentException 追踪数据

该表有一个 created_at 字段,它是一个 postgres timestampz 字段,即我猜的带有时区的时间戳。

即使我通过 $column->created_at 访问该字段

它抛出了那个错误。

【问题讨论】:

    标签: laravel laravel-5 laravel-4 php-carbon


    【解决方案1】:

    尝试在您的模型中添加getDateFormat 以覆盖默认格式:

    protected function getDateFormat()
    {
        return 'Y-m-d H:i:sO';
    }
    

    getDateFormat 方法在 Laravel 5.5 及更低版本中受到保护,但在 5.6 中它是公开的,并且在 Eloquent 部分的 upgrade guide 中提到:

    【讨论】:

    • 在我的例子中,Laravel 抛出了一个错误,说 getDateFormat 应该是公开的
    • 当你使用 Carbon 和 Postgres 时会发生这种情况,因为 Postrges 给了我们毫秒。如果您在 Postgres 中使用 TIME WITH TIMEZONES,那么这是正确的格式 Y-m-d H:i:s.uO
    猜你喜欢
    • 2014-08-01
    • 2020-04-17
    • 1970-01-01
    • 2015-06-10
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多