Search code examples
phpmysqllaravelmigrationartisan-migrate

How to create a migration in Laravel 5 that only adds a column to an existing table


I'm trying to add a number of new column to the generic 'users' table that comes with the Laravel 5 installation.

However I create a new migration that does a Schema::create as follows:

Schema::create('users', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('username');
    $table->string('email')->unique();
    $table->string('password', 60);
    $table->string('avatar');
    $table->string('first_name');
    $table->string('last_name');
    $table->string('nickname');
    $table->rememberToken();
    $table->timestamps();
});

In the above I added the avatar, first_name, last_name and nickname columns.

My problem is that when I run php artisan migrate it's telling me Nothing to migrate.

How do I create an "update" migration?


Solution

  • From http://laravel.com/docs/5.1/migrations#creating-columns

    Creating Columns

    To update an existing table, we will use the table method on the Schema facade. Like the create method, the table method accepts two arguments: the name of the table and a Closure that receives a Blueprint instance we can use to add columns to the table:

    Schema::table('users', function ($table) {
        $table->string('email');
    });
    

    Also look at this from http://laravel.com/docs/5.1/migrations#creating-columns

    Rollback / Migrate In Single Command Note: you will loose any data in the DB with this method

    The migrate:refresh command will first roll back all of your database migrations, and then run the migrate command. This command effectively re-creates your entire database:

    php artisan migrate:refresh