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?
Learn how to set the default timestamp in Laravel migration to auto assign the current timestamp When writing a migration file, you can set the default value for the timestamp column type. The options are available using the "useCurrent ()" method and the value will be set as CURRENT_TIMESTAMP as the default value.
Each migration filename contains a timestamp that allows Laravel to determine the order of the migrations: Laravel will use the name of the migration to attempt to guess the name of the table and whether or not the migration will be creating a new table.
A migration class contains two methods: up and down. The up method is used to add new tables, columns, or indexes to your database, while the down method should reverse the operations performed by the up method. Within both of these methods, you may use the Laravel schema builder to expressively create and modify tables.
Try this:
$table->timestamp('time')->useCurrent = true;
See, if that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With