php - 在 Laravel 迁移中更改列类型的最佳方法是什么?

我的数据库中有一个用户表:

        $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();

我需要将“角色”列类型更改为“文本”,然后在 Laravel 中运行新的迁移。如果我不想对以前的数据产生影响,最好的方法是什么。

最佳答案

你可以尝试:

  1. 创建一个名为“roleTemp”的新文本列
  2. 运行查询以根据“角色”列为每条记录设置此列
  3. 删除“角色”列
  4. 将“roleTemp”重命名为“role”

现在只需按原样更改数据库模式:

$table->increments('id');
$table->string('fullName');
$table->string('email')->unique();
$table->string('password', 50);
$table->string('role');
$table->rememberToken();
$table->timestamps();

基本上,您将角色值复制到(临时)列并重命名。至少它是安全的,不会花费很多时间。

https://stackoverflow.com/questions/38290815/

相关文章:

java - 在不同计算机之间同步 IntelliJ 项目

c# - 使用 Xamarin android 从 Asset 加载文件

sql - 数据库中每个表的最大值(ID)

bash - 在 byobu 选项卡中启动命令的脚本

r - 如何在 R 中将一个字符列拆分为多个列

ansible - 具有 SSH key 身份验证的用户如何在 Ansible 中拥有 sudo 权

laravel - 仅在 Laravel 中公开某些授权路由

php - PHP 中的 fatal error 是什么意思?

google-api - Google Classroom API 修改附件

php - 如何取消设置特定用户的 session ?