Laravel migrations change default value of column(Laravel 迁移更改列的默认值)
问题描述
我有一个已经分配了默认值的表.例如,我们可以查看以下内容:
I have a table with a default value already assigned. For an example we can look at the following:
Schema::create('users', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('active')->default(1);
});
我现在想更改活动字段的默认值.我期待做这样的事情:
I now want to change my default value on the active field. I am expecting to do something like this:
if (Schema::hasTable('users')) {
Schema::table('users', function (Blueprint $table) {
if (Schema::hasColumn('users', 'active')) {
$table->integer('active')->default(0);
}
});
}
但它当然告诉我该列已经存在.如何在不删除列的情况下简单地更新列 x 的默认值?
But of course it tells me the column is already there. How can I simply update the default value of column x without dropping the column?
推荐答案
你可以使用 change()
方法:
You can use change()
method:
Schema::table('users', function ($table) {
$table->integer('active')->default(0)->change();
});
然后运行 migrate
命令.
Then run migrate
command.
更新
对于 Laravel 4,使用如下内容:
For Laravel 4 use something like this:
DB::statement('ALTER TABLE `users` CHANGE COLUMN `active` `active` INTEGER NOT NULL DEFAULT 0;');
在 up()
方法中,而不是 Schema::table();
子句.
Inside up()
method instead of Schema::table();
clause.
这篇关于Laravel 迁移更改列的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 迁移更改列的默认值


基础教程推荐
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01