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

前端 未结 9 1457
执笔经年
执笔经年 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:39

    Starting from Laravel 5.1.26, tagged on 2015-12-02, a useCurrent() modifier has been added:

    Schema::table('users', function ($table) {
        $table->timestamp('created')->useCurrent();
    });
    

    PR 10962 (followed by commit 15c487fe) leaded to this addition.

    You may also want to read issues 3602 and 11518 which are of interest.

    Basically, MySQL 5.7 (with default config) requires you to define either a default value or nullable for time fields.

提交回复
热议问题