我想使用Laravel Schema Builder/Migrations创建一个默认值为CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP的时间戳列。我已经多次阅读Laravel文档,我不知道如何将其作为时间戳列的默认值。
timestamps()函数将它所生成的两列的默认值设置为0000-00-00 00:00。
我想使用Laravel Schema Builder/Migrations创建一个默认值为CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP的时间戳列。我已经多次阅读Laravel文档,我不知道如何将其作为时间戳列的默认值。
timestamps()函数将它所生成的两列的默认值设置为0000-00-00 00:00。
当前回答
假设它是一个原始表达式,你应该使用DB::raw()将CURRENT_TIMESTAMP设置为列的默认值:
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
这在每个数据库驱动程序上都可以完美地工作。
从Laravel 5.1.25(参见PR 10962和提交15c487fe)开始,你现在可以使用新的useCurrent()列修改器方法来实现列的默认值:
$table->timestamp('created_at')->useCurrent();
回到问题,在MySQL上,你也可以通过DB::raw()使用on UPDATE子句:
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
同样,在Laravel 8.36.0(参见PR 36817)中,你现在可以使用新的useCurrentOnUpdate()列修饰符方法和useCurrent()修饰符来为列实现相同的默认值:
$table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();
陷阱
MySQL Starting with MySQL 5.7, 0000-00-00 00:00:00 is no longer considered a valid date. As documented at the Laravel 5.2 upgrade guide, all timestamp columns should receive a valid default value when you insert records into your database. You may use the useCurrent() column modifier (from Laravel 5.1.25 and above) in your migrations to default the timestamp columns to the current timestamps, or you may make the timestamps nullable() to allow null values. PostgreSQL & Laravel 4.x In Laravel 4.x versions, the PostgreSQL driver was using the default database precision to store timestamp values. When using the CURRENT_TIMESTAMP function on a column with a default precision, PostgreSQL generates a timestamp with the higher precision available, thus generating a timestamp with a fractional second part - see this SQL fiddle. This will led Carbon to fail parsing a timestamp since it won't be expecting microseconds being stored. To avoid this unexpected behavior breaking your application you have to explicitly give a zero precision to the CURRENT_TIMESTAMP function as below: $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP(0)')); Since Laravel 5.0, timestamp() columns has been changed to use a default precision of zero which avoids this.
感谢@andrewhl指出Laravel 4。X问题在评论中。
感谢@ChanakaKarunarathne在评论中提出了新的useCurrentOnUpdate()快捷方式。
其他回答
在Laravel 5中:
$table->timestamps(); //Adds created_at and updated_at columns.
文档:http://laravel.com/docs/5.1/migrations creating-columns
它将100%工作。通过这种方式,您可以在更改列的datetype后设置默认的datetime值。
Schema::table('table_name', function (Blueprint $table) {
$table->dateTime('ans_time')->default(now())->change();
});
我在Laravel中使用以下自定义:
默认created_at为当前时间戳 更新记录时更新时间戳
首先,我将在Laravel的根目录下创建一个helpers.php文件,并插入以下内容:
<?php
if (!function_exists('database_driver')) {
function database_driver(): string
{
$connection = config('database.default');
return config('database.connections.' . $connection . '.driver');
}
}
if (!function_exists('is_database_driver')) {
function is_database_driver(string $driver): bool
{
return $driver === database_driver();
}
}
在作曲家。json,我将插入以下到自动加载。这允许编写器自动发现helpers.php。
"autoload": {
"files": [
"app/Services/Uploads/Processors/processor_functions.php",
"app/helpers.php"
]
},
我在Laravel模型中使用以下方法。
if (is_database_driver('sqlite')) {
$table->timestamps();
} else {
$table->timestamp('created_at')->default(\DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')
->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
}
这允许我的团队继续使用sqlite进行单元测试。ON UPDATE CURRENT_TIMESTAMP是MySQL的快捷方式,在sqlite中不可用。
这就是你怎么做的,我已经检查过了,它在我的Laravel 4.2上工作。
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
希望这能有所帮助。
唯一对我有用的是
DB::statement("ALTER TABLE orders CHANGE updated_at updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP");