在laravel迁移中使用数据更改列中的数据类型

在laravel迁移中使用数据更改列中的数据类型,laravel,database-migration,Laravel,Database Migration,这就是迁移?我必须使用现有数据将字符串数据列更改为整数数据列 public function up() { Schema::table('SYS_tenants' ,function (Blueprint $table){ $table->integer('tena_type')->unsigned()->nullable()->change(); $table->foreign('tena_type')-&

这就是迁移?我必须使用现有数据将字符串数据列更改为整数数据列

public function up()
{
       Schema::table('SYS_tenants' ,function (Blueprint $table){
           $table->integer('tena_type')->unsigned()->nullable()->change();
           $table->foreign('tena_type')->references('id')->on('account_types');
       });
}

看起来你所拥有的应该有用:

Schema::table('SYS_tenants' ,function (Blueprint $table){
    $table->integer('tena_type')->unsigned()->nullable()->change();
});


根据您的数据库,您可能需要将值强制转换为新类型:(对于mysql:)

在设置新字段类型后,您可以对要更改类型的字段使用
change
方法

public function up() {
    Schema::table('SYS_tenants' ,function (Blueprint $table){

        $table->string('tena_type')->change();   
    });
}
我假设创建表的迁移已经调用了所有您需要的需求,比如
unique
nullable
等等。您可以调用
change
方法,顺便说一句,您想要执行的任何修改都没有限制,比如在该字段上添加其他mysql索引


不要忘记在
composer.json
文件中添加
原则/dbal
,根据laravel文档,您可以创建一个新的迁移,并按如下方式执行:

Schema::table('SYS_tenants', function (Blueprint $table) {
   $table->integer('tena_type')->unsigned()->nullable()->change();
});
在修改列之前,请确保添加
原则/dbal
依赖项 到您的
composer.json
文件


参考资料:

我已经在使用此Laravel迁移

$table->integer('tena_type')->unsigned()->nullable()->change()

但是它不起作用,因为数据已经在表中了。在这种情况下,它无法更改数据类型。我使用此DB语句更改带有数据的数据类型。它工作正常


DB::语句(“alter table SYS\U租户修改tena\U type integer not null”):

要更改哪种列类型?
composer require doctrine/dbal