Search code examples
phplaravellaravel-5php-carbon

InvalidArgumentException in Carbon.php line 425: Trailing data


I just recently migrate from MySQL to PostgreSQL. I noticed I kept getting this error

InvalidArgumentException in Carbon.php line 425:
Trailing data

Then, I did a global search for Carbon:: in my application. I found 47 matches across 15 files.

How do I fix this errors without having to modify all those files ?


I dd() my object this is what I got

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"
  ]

   ....

Solution

  • It's exactly what you think. The issue here is that the timestamp column in PostgreSQL is actually expecting a different timestamp format which includes the miliseconds at the end. 'Y-m-d H:i:s.u' as opposed to Carbon's default format and MySQL's timestamp column which is just 'Y-m-d H:i:s. In order to fix this, tell PostgreSQL that you don't want to store the Timezone as part of your strings in your timestamp columns:

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

    You'll need to do this for all tables and all columns which have a timestamp.