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

ByteShaman's avatar

Set default value to a date field (in migration)

As the title says I'd like to set a default value for a date field inside the migration.

I can't change the date type to datetime/timestamp type.

What i tried:

$table->date('data')->default(new Expression('(CURDATE())'))

$table->date('data')->default(new Expression('CURDATE()'))

$table->date('data')->default(DB::raw('CURDATE()'))

I always get the same error: 1067 Invalid default value for 'data'

What am I doing wrong? Thanks in advance.

0 likes
17 replies
aurawindsurfing's avatar
$table->date('data')->default(date("Y-m-d H:i:s"));

It has to be timestamp.

tykus's avatar

@aurawindsurfing that would result in a fixed date (now) as the default, whereas the OP needs CURRENT_TIMESTAMP.

ByteShaman's avatar

@aurawindsurfing : that seems to work, but would it be a different date for each day I add a new record? Or will it always use the same value?

EDIT: oh as I thought it'll be a fixed date, yeah as @tykus said I need to emulate the CURDATE() function of MySQL. I thought Expression was the way to go, but maybe I misunderstood what it does...

tykus's avatar

You need ->default(DB::raw('CURRENT_TIMESTAMP')) @n00bcod3r - otherwise, the migration will fix the datetime that the migration was run.

ByteShaman's avatar

@tykus I'm getting this error:

SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'data' (SQL: ALTER TABLE selezioni_stampa CHANGE data data DATE DEFAULT 'CURRENT_TIMESTAMP' NOT NULL)

I think it's because the type is date and not datetime? That's why I wanted to use the CURDATE() function, but I still get the same error

PS: I'm using a ->change() function that's why it's an ALTER TABLE and not a CREATE

tykus's avatar

You are using the Carbon now() @aurawindsurfing - it will be evaluated (in PHP) at runtime to a fixed datetime.

ByteShaman's avatar

Anyway I don't this can be achieved, since I can't change the type to Datetime... I've done some tests from PHPMyAdmin and it's not possible to set it as CURDATE() or CURRENT_TIMESTAMP

tykus's avatar
tykus
Best Answer
Level 104

What is your mysql version? For <8.0.13, your default is not able to take an expression

With one exception, the default value specified in a DEFAULT clause must be a literal constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that, for TIMESTAMP and DATETIME columns, you can specify CURRENT_TIMESTAMP as the default.

https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html

tykus's avatar

Why not use a TIMESTAMP data type?

ByteShaman's avatar

The DB owner told me I cannot change the type...

tykus's avatar

The DB owner told me I cannot change the type...

But you can upgrade the version?

ByteShaman's avatar

Well actually I'm using an older version in my local machine, so I wouldn't change the version since they always keep it up to date... Having troubles upgrading the version in my machine but that's not the topic so I'll ask somewhere else :)

cob05's avatar

I know that this is old, but I was recently facing the same issue.

These both worked for me:

        $table->date('registration_date')->default(DB::raw('(CURDATE())'));
or
        $table->date('registration_date')->default(new Expression('(CURDATE())'));

MySQL doesn't like that Laravel omits the surrounding parenthesis for default values of expressions.

1 like
Benjaminhu's avatar

I ran into a similar problem:

SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'track_release_date'

I found a solution in the documentation (I use Laravel 8 and MySQL 8): https://laravel.com/docs/8.x/migrations#column-modifiers

->useCurrent()              Set TIMESTAMP columns to use 
                            CURRENT_TIMESTAMP as default value.
->useCurrentOnUpdate()      Set TIMESTAMP columns to use 
                            CURRENT_TIMESTAMP when a record is updated.

this ran successfully:

$table->dateTime('track_release_date')->useCurrent();

Please or to participate in this conversation.