Change Table Columns With Migration In Laravel 5 Or Above

Recently I have started working on a Laravel project and playing with migration for the first time and realized that managing the database with migration, it very easy to handle. In this tutorial, I will explain how to create the migration and how to change columns type.

Let’s create a games table with the migration, so first open your terminal and navigate to your project directory and hit below command:

php artisan make:migration create_games_table

The new migration will be placed in your database/migrations directory. Now open your games migration file and add Schema to your up method:

Schema::create('games', function (Blueprint $table) {
    $table->integer('id', '11')->increments();

Run migration command to generate your games table:

php artisan migrate

Now after creating a table, I realized that I have done a mistake in the title field and I used the title as an integer type and it must have string type so now what to do. Well, it’s very easy to replace the type with the migration.

Create a new migration and add Schema to your up method:

Schema::table('games', function($table)

You just need to add change() function at the end of the chain and it will correct your type.

If you are getting below error after running migration don’t afraid.

Changing columns for table "games" requires Doctrine DBAL; install "doctrine/dbal".

You must install doctrine/dbal so type composer require doctrine/dbal in your terminal.

Happy Coding 😉