Laravel迁移将列类型从varchar更改为longText

Mar*_*rco 19 laravel laravel-migrations

我需要将迁移列类型更改$table->string('text');为文本类型,我尝试以几种方式执行此操作,但它们都不起作用.是否可以在一次迁移中完成.我可以猜测删除列然后再使用新类型创建它,但我想知道是否可以在一次迁移中执行此操作?

Ale*_*nin 31

是的,您可以创建新的迁移并仅更改一种列类型:

public function up()
{
    Schema::table('sometable', function (Blueprint $table) {
        $table->text('text')->change();
    });
}
Run Code Online (Sandbox Code Playgroud)

  • 您还需要放弃该字段吗?因为在使用代码更改列时,我仍然收到“列已存在:1060重复的列名”。 (2认同)

Jus*_*and 18

可以使用 TABLE 迁移。

正如其他帖子中提到的,一定composer require doctrine/dbal要从你的项目根目录运行。

这些设置为:

php artisan make:migration alter_table_[yourtablenamehere]_change_[somecolumnname] --table=[yourtablenamehere]
Run Code Online (Sandbox Code Playgroud)

从你的项目根。

从文档:

https://laravel.com/docs/master/migrations#modifying-columns

class AlterTableSomething extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('table', function (Blueprint $table) {
            $table->text('column_name')->change();
        });
    }
}
Run Code Online (Sandbox Code Playgroud)


Kam*_*ski 11

如果您使用以下错误 change()

请求的数据库类型未知,Doctrine\DBAL\Platforms\MySQL80Platform 可能不支持。

这意味着在您的表中存在一些具有枚举类型的列(不一定是更改的列)。因此,change()您可以使用以下功能来代替使用:

public function changeColumnType($table, $column, $newColumnType) {                
    DB::statement("ALTER TABLE $table CHANGE $column $column $newColumnType");
} 
Run Code Online (Sandbox Code Playgroud)

并像这样使用它: $this->changeColumnType('sometable','text','TEXT');


zor*_*orx 6

根据Laravel Doc

你可以这样做

Schema::table('yourTable', function (Blueprint $table) {
    $table->text('text')->change();
});
Run Code Online (Sandbox Code Playgroud)

一定要将doctrine/dbal依赖项添加到composer.json文件中