php

Laravel: How to add column to an existing table

Problem

Adding new columns to your Laravel migrations is a common thing. Applications are meant to be scalable which means that adding new features develops your application further.

Very often, new Laravel developers need to run their migrations but they don’t make sure that migrations can rollback and migrate multiple times without breaking anything.

While that’s not our focus, I think it was important to state that before we head into our problem.

The following is a common issue that new Laravel developers will try to do when they want to add a new column to an existing table.

So at this point they already did something like:

public function up()
{
    Schema::create(‘organizations’, function ($table) {
        $table->increments(‘id’);
        $table->string(‘name’)->nullable();
        $table->text(‘about’)->nullable();
    });
}

This will create a new table for them. And to make this clean, you should also add the down function and just drop your whole table in this case. The down function will be run when you want to rollback your migration.

Jumping through all of that, the real problem shows up when they forget one column and they want to add that afterwards so they create a new migration file (class) try to run something like:

public function up()
{
    Schema::create(‘organizations’, function ($table) {
        $table->integer(‘size’)->nullable();
    });
}

They are hoping to add a new column size to the existing table.

Now let’s see what happens and how to prevent that from happening again.

Solution

The main problem here is what new developers often tend to miss noticing which is the static method name of the Schema . You only use create when you are initially creating your table. If you need to further update your table at any time, you want to use table instead.

So the real up function should be like this:

public function up()
{
    Schema::table(‘organizations’, function ($table) {
        $table->integer(‘size’)->nullable();
    });
}

And the down function would be like this:

public function down()
{
    Schema::table(‘organizations’, function($table) {
        $table->dropColumn(‘size’);
    });
}

My personal suggestion to you is that after you create your new (altering) migration file do the following:

  • Run the migration
  • Check if the column is added to the table
  • Rollback the migration by running php artisan migrate:rollback
  • Make sure that nothing happens
  • Repeat step 2 and 3 again to make sure you can close the full circle of migratio
  • n

Another tip

This will come handy at a later stage if you want to automate your deployment and your script needs to run a rollback.

Another tip that I can give you is to plan where you want to place your column. Just by doing this, Laravel will place your new column at the end, probably after the updated_at column. (Most tables have this)

You want to use a method after so your final code would look like this:

public function up()
{
    Schema::table(‘organizations’, function ($table) {
        $table->integer(‘size’)->after(‘name’)->nullable();
    });
}

In this case, Laravel will place your new column right after the name column so it looks nice and is organized much better.

About the author

laravelrecipies