moh*_*moh 3 php laravel laravel-5 laravel-migrations
我的数据库中有一个用户表:
$table->increments('id');
$table->string('fullName');
$table->string('email')->unique();
$table->string('password', 50);
$table->enum('role',['boss','employee','customer'])->default('customer');
$table->rememberToken();
$table->timestamps();
Run Code Online (Sandbox Code Playgroud)
我需要将“角色”列类型更改为“文本”,然后在 Laravel 中运行新的迁移。如果我想对以前的数据没有影响,最好的方法是什么。
您可以尝试:
现在只需更改您的数据库架构即可:
$table->increments('id');
$table->string('fullName');
$table->string('email')->unique();
$table->string('password', 50);
$table->string('role');
$table->rememberToken();
$table->timestamps();
Run Code Online (Sandbox Code Playgroud)
基本上,您将角色值复制到(临时)列并重命名。至少是安全的,而且不会花很多时间。
很简单,如下所示,用这一行创建一个新的迁移。请注意该->change()函数,它使其成为 ALTER 查询。
Schema::table('usertable', function (Blueprint $table) {
$table->string('role')->default('author')->change();
});
Run Code Online (Sandbox Code Playgroud)
请记住,string() 的默认大小为 255,如果您的枚举值大于该值,它会将枚举值截断为 255 个字符。
更新:
您需要安装doctrine/dbal才能工作,请参阅Laravel 迁移 - 修改列
要安装doctrine/dbal,只需执行以下操作composer require doctrine/dbal
注意:当前不支持修改表中也有枚举类型列的任何列。
| 归档时间: |
|
| 查看次数: |
7013 次 |
| 最近记录: |