Laravel migrations change a column type from varchar to longText

前端 未结 5 1946
野性不改
野性不改 2021-02-02 06:49

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 poss

相关标签:
5条回答
  • 2021-02-02 07:36

    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

    0 讨论(0)
  • 2021-02-02 07:40

    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');

    0 讨论(0)
  • 2021-02-02 07:47

    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
    

    Works with Laravel 5.0+. It does not work with Laravel 4.2.

    0 讨论(0)
  • 2021-02-02 07:48

    It's possible to do with a TABLE migration.

    As mentioned in other posts, be sure to run composer require 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();
            });
        }
    }
    
    0 讨论(0)
  • 2021-02-02 07:51

    Following worked for me.

    Definitely you need to install doctrine/dbal to make this work, using following command in terminal.

    composer require doctrine/dbal
    

    Then create migration as mentioned here- https://laravel.com/docs/master/migrations#generating-migrations

    open your migration file and write down below.

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

    As, longText have maximum of 4,294,967,295 character limit, Laravel will automatically change column_name to longText data type.

    0 讨论(0)
提交回复
热议问题