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

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

    As additional possibility for future googlers

    I find it more useful to have null in the updated_at column when the record is been created but has never been modified. It reduces the db size (ok, just a little) and its possible to see it at the first sight that the data has never been modified.

    As of this I use:

    $table->timestamp('created_at')->useCurrent();
    $table->timestamp('updated_at')->default(DB::raw('NULL ON UPDATE CURRENT_TIMESTAMP'))->nullable();
    

    (In Laravel 7 with mysql 8).

提交回复
热议问题