Carbon.php 第 425 行中的 InvalidArgumentException:尾随数据

InvalidArgumentException in Carbon.php line 425: Trailing data

我最近刚从 MySQL 迁移到 PostgreSQL。 我注意到我一直收到此错误

InvalidArgumentException in Carbon.php line 425:
Trailing data

然后,我在我的应用程序中对 Carbon:: 进行了全局搜索。我在 15 个文件中找到了 47 个匹配项。

如何在不修改所有这些文件的情况下修复此错误?


dd()我的对象这就是我得到的

Capture {#412 ▼
  #table: "captures"
  #connection: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:6 [▼
    "id" => 65
    "type" => "cpe"
    "cpe_mac" => "000D6721A5EE"
    "device_mac" => null
    "created_at" => "2016-05-03 11:20:10-04"
    "updated_at" => "2016-05-03 11:20:10-04"
  ]

   ....

这正是您的想法。这里的问题是 PostgreSQL 中的 timestamp 列实际上需要一种不同的时间戳格式,其中包括末尾的 miliseconds'Y-m-d H:i:s.u' 与 Carbon 的默认格式和 MySQL 的时间戳列相反,后者只是 'Y-m-d H:i:s。为了解决这个问题,请告诉 PostgreSQL 您不想将时区作为字符串的一部分存储在 timestamp 列中:

ALTER TABLE {tablename} ALTER updated_at SET DATA TYPE timestamp(0) without time zone;

您需要对具有时间戳的所有表和所有列执行此操作。

意识到这是一个旧的 post,但我已经通过创建全局范围解决了这个问题。参见:https://laravel.com/docs/5.6/eloquent#global-scopes

然后我做了:

$builder->addSelect(['select','cols','here',to_char(created_at,'YYYY-MM-DD HH24:MI:SS') 作为created_at"];

这意味着可以与 Laravel 一起正常工作。因为我只是从这个数据库中读取数据,所以不需要考虑更新时间戳。

由于这是 Carbon 错误,因为您可能需要以毫秒为单位的日期。

"created_at" => "2018-04-19 07:01:19.929554"

您可以将以下方法添加到您的(基本)模型中。

public function getDateFormat()
{
     return 'Y-m-d H:i:s.u';
}

如果您在 Postgres 中使用 TIME WITH TIMEZONE,则格式应为:

Y-m-d H:i:s.uO