database icon indicating copy to clipboard operation
database copied to clipboard

Laravel + Postgresql timestamp with timezone

Open EspadaV8 opened this issue 9 years ago • 6 comments

Using Postgres timestamp with time zone column with Laravel causes an issue with Carbon

InvalidArgumentException in Carbon.php line 414:
Trailing data

This is because the data returned by Postgres include the +00 timezone information on the end. One fix would be to override getDateFormat and have it return 'Y-m-d H:i:sO' but of course we'd only want to do that where timezones are being used.

Models can override their own dateFormat property, but pivot tables with timestamps can't do this (since there's no actual model for them).

EspadaV8 avatar Jul 17 '15 04:07 EspadaV8

:+1:

matejvelikonja avatar Feb 23 '16 08:02 matejvelikonja

Are there any updates on this issue ? We're facing the same problem.

PatrickHoopman avatar Mar 23 '16 17:03 PatrickHoopman

I had forgotten about this. You can actually create a subclass of the Pivot class and set the timestamp format there. Then in a subclass of Laravel Model class you can override the newPivot method to return an instant of your own Pivot.

It's a bit of a mess to get working, but it should do the job. We've actually moved away from using pivots at all and instead give the Pivot tables their own eloquent model class since it's just easier to work with them that way.

EspadaV8 avatar Mar 23 '16 20:03 EspadaV8

any update?

parabol avatar Nov 24 '16 07:11 parabol

You just need to set your dateFormat in the Model class accordingly:

protected $dateFormat = 'Y-m-d H:i:sO';

mirzap avatar Apr 13 '17 05:04 mirzap

shows below error :The timezone could not be found in the database

nareshsolulab avatar Jul 22 '19 12:07 nareshsolulab