Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change a column type with Laravel schema builder?

I need to change two fields from integer to foreign key. How do I build my migration to do so?

Schema::create('messages', function($table)
{
    $table->increments('id');
    $table->integer('sender');
    $table->integer('recipient');
    $table->string('title');
    $table->longtext('body');
    $table->timestamps();
    $table->softDeletes();
    $table->integer('regarding');
});

I'll change sender to sender_id, recipient to recipient_id and regarding to regarding_id.

like image 496
mike Avatar asked Feb 27 '14 06:02

mike


People also ask

How do I change a column to unique in laravel migration?

$table->unique('slug'); So you add unique index to existing 'slug'.

How do you make a column nullable in laravel?

The code to change the column to nullable is as "nullable()->change()". ->nullable()->change(); For this example, we'll be using the route body and we will make the "posts" table "user_id" to be nullable.


2 Answers

Don't think too much about it, I'm using following code. Here I change the data type from varchar to text.

public function up(){
    DB::statement('ALTER TABLE items MODIFY COLUMN item_description TEXT');
    DB::statement('ALTER TABLE items MODIFY COLUMN delivery_description TEXT');
}

public function down(){
    DB::statement('ALTER TABLE items MODIFY COLUMN item_description VARCHAR(255)');
    DB::statement('ALTER TABLE items MODIFY COLUMN delivery_description VARCHAR(255)');
} 
like image 95
Kajan Thadsanamoorthy Avatar answered Oct 12 '22 22:10

Kajan Thadsanamoorthy


I came across a similar problem needing to change a column type from string to integer. I managed this using two separate migrations (with a RAW sql statement in each) to get the solution.

In addition this works with Postgres and MySQL because we were in the middle of a transfer.

The first migration:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('plans', function(Blueprint $table)
    {
        //
        $table->mediumInteger('duration_change_type')->default(0)->after('duration');
    });

    if (Config::get('database')['default'] === 'mysql'){
        // Mysql 
        DB::statement('update plans set duration_change_type=duration');

    } else if (Config::get('database')['default'] === 'pgsql'){
        // PostgreSQL
        DB::statement('update plans set duration_change_type=duration::integer');
    }
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('plans', function(Blueprint $table)
    {
        //
        $table->dropColumn('duration_change_type');
    });
}

The second migration:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('plans', function(Blueprint $table)
    {
        //
        $table->dropColumn('duration');
        $table->renameColumn('duration_change_type', 'duration');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('plans', function(Blueprint $table)
    {
        // Rollback to string
        $table->string('duration_change_type')->default(0)->after('duration');
    });
    if (Config::get('database')['default'] === 'mysql'){
        // Mysql 
        DB::statement('update plans set duration_change_type=duration');

    } else if (Config::get('database')['default'] === 'pgsql'){
        // PostgreSQL
        DB::statement('update plans set duration_change_type=duration::text');
    }
}

Thinking on this now it could be simplified to a single migration.

like image 25
Ben Avatar answered Oct 12 '22 23:10

Ben