Laravel 迁移:使用现有列的默认值添加列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37184492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Migration: Add a column with a default value of an existing column
提问by Parvez Rahaman
i need to add a last_activity column on a table of a live site where i have to make it's default value to equal of updated_at column value. I'm using laravel 5.1 and mysql database driver.
我需要在实时站点的表上添加一个 last_activity 列,我必须使其默认值等于 updated_at 列值。我正在使用 laravel 5.1 和 mysql 数据库驱动程序。
Schema::table('users', function(Blueprint $table)
{
$table->dateTime('last_activity')->default("COLUMN VALUE OF UPDATED_AT");
});
Thanks.
谢谢。
回答by tptcat
This should work:
这应该有效:
Schema::table('users', function(Blueprint $table) {
$table->dateTime('last_activity');
});
\DB::statement('UPDATE users SET last_activity = updated_at');
See "Running a General Statement": https://laravel.com/docs/5.1/database#database-transactions
请参阅“运行一般语句”:https: //laravel.com/docs/5.1/database#database-transactions
To enforce this relationship when a User
is created or updated, you can use Model Eventsand/or Model Observers.
要在User
创建或更新a 时强制执行此关系,您可以使用Model Events和/或Model Observers。