Add a new column to an already existing table

As a project gets developed more and more there will be changes needed in the database structure too. So, how will you add a new column to an existing table in the database?. Let's see how to do it with an example. Suppose your table's name is 'userdetails' its already created and you want to add one more column named 'active'. There is an artisan command for this.

php artisan make:migration add_active_to_userdetails

Then you need to use the schema table method. Add the new column name to the table.

public function up()

{

    Schema::table('userdetails', function($table) {

        $table->integer('active');

    });

}

Now you should add a rollback method also as given below.

public function down()

{

    Schema::table('userdetails', function($table) {

        $table->dropColumn('active');

    });

}

At last, execute the migrate command.

php artisan migrate