How to Change Column Length using Laravel Migration?

By Hardik Savani | April 28, 2021 | 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...




Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: