Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default DateTime() in Laravel migrations? [duplicate]

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?

like image 663
Alex Lomia Avatar asked May 10 '16 12:05

Alex Lomia


People also ask

How to set the default timestamp in Laravel migration?

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.

How does the Order of migrations work in Laravel?

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.

What is up and down method in Laravel migration?

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.


1 Answers

Try this:

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

See, if that helps.

like image 89
Gaurav Dave Avatar answered Sep 30 '22 16:09

Gaurav Dave