How to set default DateTime() in Laravel migrations?

Alex Lomia picture Alex Lomia · May 10, 2016 · Viewed 24.8k times · Source

I am trying to achieve the functionality illustrated below:

$table->dateTime('time')->default(new \DateTime());

This exact code is failing, because

[ErrorException]
Object of class DateTime could not be converted to string

The other problem is, that I feel that there should be more robust/elegant way of solving this issue. So, how to correctly set a default DateTime value in migrations?

Answer

Gaurav Dave picture Gaurav Dave · May 10, 2016

Try this:

$table->timestamp('time')->useCurrent = true;

See, if that helps.