How Can I Set the Default Value of a Timestamp Column to the Current Timestamp with Laravel Migrations?

前端 未结 9 1449
执笔经年
执笔经年 2020-11-27 10:21

I would like to make a timestamp column with a default value of CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP using the Laravel Schema Builder/Migrations. I hav

9条回答
  •  再見小時候
    2020-11-27 10:32

    This doesn't work for a fact:

    $table->timestamp('created_at')->default('CURRENT_TIMESTAMP');
    

    It doesn't remove the 'default 0' that seems to come with selecting timestamp and it just appends the custom default. But we kind of need it without the quotes. Not everything that manipulates a DB is coming from Laravel4. That's his point. He wants custom defaults on certain columns like:

    $table->timestamps()->default('CURRENT_TIMESTAMP');
    

    I don't think it's possible with Laravel. I've been searching for an hour now to see whether it's possible.


    Update: Paulos Freita's answer shows that it is possible, but the syntax isn't straightforward.

提交回复
热议问题