Home >Database >Mysql Tutorial >How to Set a Timestamp Column\'s Default Value to CURRENT_TIMESTAMP in Laravel Migrations?
How can I create a timestamp column with a default value of CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP using Laravel Schema Builder/Migrations?
Using DB::raw() (all databases):
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
Using useCurrent() (Laravel 5.1.25 ):
$table->timestamp('created_at')->useCurrent();
For MySQL only:
Using DB::raw() with ON UPDATE:
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
Using useCurrent() and useCurrentOnUpdate() (Laravel 8.36.0 ):
$table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();
Gotchas:
MySQL:
PostgreSQL & Laravel 4.x:
The above is the detailed content of How to Set a Timestamp Column\'s Default Value to CURRENT_TIMESTAMP in Laravel Migrations?. For more information, please follow other related articles on the PHP Chinese website!