92

I need to change with a migration column type of $table->string('text'); to a text type, I have tried to do that in a 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 a new type, but I wonder if it is possible to do it in one migration?

Ludwig
  • 1,401
  • 13
  • 62
  • 125

6 Answers6

163

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.

Debiprasad
  • 5,895
  • 16
  • 67
  • 95
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • 2
    do you still need to drop the field? because I am still getting "Column already exists: 1060 Duplicate column name" when using your code to change a column. – NewbieLearner Jun 30 '16 at 10:46
  • 1
    @NewbieLearner This feature is available since Laravel 5.0. If you are using Laravel 4.2, this won't work. – Debiprasad Mar 11 '20 at 10:51
43

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();
        });
    }
}
Community
  • 1
  • 1
Justin Origin Broadband
  • 1,492
  • 1
  • 11
  • 14
25

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

zorx
  • 1,861
  • 14
  • 16
12

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

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
4

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.

Vatsal Shah
  • 1,334
  • 18
  • 21
0

To do this, you need doctrine/dbal, that's true.

BUT, be careful! Otherwise you may end up in trouble.

Here's a safer way

  1. Backup your project files!

  2. Take a look at your composer.json file to note your PHP version. Let's say you have 7.2.5. Now, search for a compatible version which is 2.13.9 in this case.

Version 2.13.9

  1. If you do not want to mess with other dependencies, make sure you install this package using this command
COMPOSER_MEMORY_LIMIT=-1 composer require doctrine/dbal "^2.13.9"
  1. Now we're ready to make changes!
  • Generate your migration file
php artisan make:migration change_xxx_type_in_yyy_table --table=yyy
  • Change its content according to your needs
// Let's assume we change an integer column's type to string
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
  Schema::table('yyy', function (Blueprint $table) {
    $table->string('xxx')->change();
  });
}

/**
 * Reverse the migrations.
 *
 * @return void
*/
public function down()
{
  Schema::table('yyy', function (Blueprint $table) {
    $table->integer('xxx')->change();
  });
}
  • Run your migration
php artisan migrate
Ahmet Firat Keler
  • 2,603
  • 2
  • 11
  • 22