How to Change Column Length using Laravel Migration?

By Hardik Savani November 5, 2023 Category : Laravel

This simple article demonstrates of change column length laravel migration. We will use laravel migration change column length. Here you will learn laravel change column length migration. I’m going to show you about migration change column length in laravel.

we are managing proper length of data type then it's very help full for db storage. we can save space. but if you added length like 50 with string data type and latter you need to upgrade it then how you will do. here is a simple example how to update column length in laravel migration.

let's see bellow example:

Migration:

<?php

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('posts', function (Blueprint $table) {

$table->bigIncrements('id');

$table->string('title', 50);

$table->text('body');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('posts');

}

}

Change Column Length using Migration

now, we need to update title string 50 length to 100. we can do it with following migration:

Install Composer Package:

composer require doctrine/dbal

Migration:

<?php

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class UpdatePostsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::table('posts', function (Blueprint $table) {

$table->string('title', 100)->change();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

}

}

now you can run it.

i hope it can help you...

Shares