Laravel migrations change a column type from varchar to longText

China☆狼群 提交于 2019-12-21 03:18:13

问题


I need to change with migration column type of $table->string('text'); to a text type, I have tried to do that in few ways, but none of them worked. Is it possible to do it in one migration. I could I guess drop the column and then create it again with new type, but I wonder if it is possible to do it in one migration?


回答1:


You can create a new migration and change just one column type:

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

You need to install doctrine/dbal to make this work

composer require doctrine/dbal



回答2:


According to Laravel Doc

You can do it like

Schema::table('yourTable', function (Blueprint $table) {
    $table->text('text')->change();
});

be sure to add the doctrine/dbal dependency to your composer.json file




回答3:


It's possible to do with a TABLE migration.

As mentioned in other posts, be sure to run composer install doctrine/dbal from your project root.

These are set up with:

php artisan make:migration alter_table_[yourtablenamehere]_change_[somecolumnname] --table=[yourtablenamehere]

from your project root.

From the Documentation:

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



回答4:


If you get following error using change()

Unknown database type enum requested, Doctrine\DBAL\Platforms\MySQL80Platform may not support it.

this means that exists some column (not necessarily changed one) in your table which has enum type. So instead using change() you can use following function:

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

And use it like that: $this->changeColumnType('sometable','text','TEXT');



来源:https://stackoverflow.com/questions/37724150/laravel-migrations-change-a-column-type-from-varchar-to-longtext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!