Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dejan7's avatar

Date mutators throw exception if i change $dateFormat

I've set $dates and $dateFormat on my model ( http://laravel.com/docs/5.1/eloquent-mutators#date-mutators )

The funny this is that if i change $dateFormat to anything other than protected $dateFormat = 'Y-m-d H:i:s' it gives me the following exception whenever i try to display the time $model->created_at->getTimestamp()

It results in this:

ErrorException in Carbon.php line 414:
Trailing data (View: /home/vagrant/Projects/kpst/resources/views/polls/index.blade.php)

And by anything, i mean literally anything. Even just protected $dateFormat = 'Y-m-d' will throw the exception

Am i doing something wrong?

0 likes
2 replies
dharmendrajadon's avatar

Because you are using data-type timestamp which in turn is an instance of time in "Y-m-d H:i:s Z". You can use some column with data-type "date" but created_at follows the exact same as mentioned above.

mikemand's avatar

I think $dateFormat is mis-named. It should probably be called $storageDateFormat or something like that, because that's what it is: the format for stored dates. When you submit a form with a date in it (or if you've changed the created_at, updated_at, or deleted_at columns to be 'Y-m-d' instead of 'Y-m-d H:i:s') Carbon will use $dateFormat to try and parse the date (both when setting and getting) and if it comes across something abnormal (in this case, the time part ('H:i:s') of the datetime stamp) it throws an exception.

I've found this incredibly annoying, and typically just use a mutator to parse dates to the required format for that particular field.

Please or to participate in this conversation.