gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:42:49 25 4
gpt4 key购买 nike

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

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

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

关于php - 在 Laravel 迁移中更改列类型的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38290815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com