Matt Trask

Two ways to fix the Laravel Postgres Timestamp issue

January 03, 2017 | 1 Minute Read

Laravel vs Postgres.

Tonight we were working on an issue with timestamps coming from our Postgres database and rendered in Laravel. However we noticed that Carbon hated them and would throw exceptions rather then just manipulate our timestamps. We looked around and found a few ideas, but the one we liked most was this:

public $timestamps = false;

and it disables the getDateFormat() method from Eloquent. The reason an issue is caused is that the getDateFormat() is built for MySQL. Since Postgres handles timestamps a bit differently, it gets screwed up when run through the getDateFormat() method. By setting the timestamp value to false, you are passing a raw Unix timestamp up to Carbon which Carbon can interpret and manipulate.

One more way

Another way to solve this issue is a little more complex, but easily implemented. It would be to create a model called PostGresModel and extend the Illuminate\Database\Eloquent\Model. In this new model class, which you will extend all your models from now, you would create a new getDateFormat() method and inside it, return 'Y-m-d H:i:s.u' which is how you handle timestamps with Postgres. The whole model would look liked this:


namespace App;

use Illuminate\Database\Eloquent\Model;

class PostgresModel extends Model
    public function getDateFormat()
      return 'Y-m-d H:i:s.u';

And there you go! We override the base Eloquent getDateFormat() method to handle Postgres, and now its publicaly availble to your models as long as you extend the new PostgresModel.

Hopefully these two solutions will help others in the same spot!

If you liked this post, you can share it with your followers or follow me on Twitter!