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

jgravois's avatar

Laravel 4 -- Set Current Date as Default in a migration file

I need a column in a database table that should default to the current date. I need this column despite there being a "created_at" column because while it should default to the current date, it has to be modifiable.

I tried $table->date('app_date')->default(now()) but that wasn't pretty.

0 likes
11 replies
chrisgeary92's avatar

Try:

$table->date('app_date')->default(DB::raw('CURRENT_TIMESTAMP'));

or maybe?

$table->date('app_date')->default(DB::raw('NOW()'));
1 like
bashy's avatar
bashy
Best Answer
Level 65

If you import Carbon, you can use this as well as the above

$table->date('app_date')->default(Carbon::now());
1 like
amitshahc's avatar

@bashy This will take the date of migration forever by default. not current date.

3 likes
bashy's avatar

@amitshahc At the time this was posted, it was believed they wanted the date to be the current date of migration.

jgravois's avatar

@chrisgeary92, both options created a "[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'app_date'".

I will try Carbon.

bestmomo's avatar

Another way :

$table->date('app_date')->default(date("Y-m-d H:i:s"));
1 like
nitosoft's avatar

solution:

$table->timestamp('name_custom')->default(\DB::raw('CURRENT_TIMESTAMP'));

1 like
pintilei's avatar

I know this is a very old post but this will not work as expected because the date is hardcoded. It will set the same default date (i.e: 2020-03-12) for every new row (because it's the day when the migration was executed, returned by Carbon).

The correct way to do that is by using a datetime column or to make sure the column is filled everytime with current date from PHP.

3 likes
eranda's avatar

$table->date('app_date')->useCurrent() will do the job

1 like

Please or to participate in this conversation.