Rizwan Saleem
Rizwan Saleem

Reputation: 896

Laravel migration add colum issue in existing database table

I am working in laravel 4.2 project and now i want to add column in existing table users. Now i want to add another column and when i run migration command then I always receive the same message "Nothing to migrate." Below is my migration modal code

public function up()
{
    Schema::table('users', function($table) {
        $table->string('dob')->nullable()->change();
    });
}

Commant that I run in terminal

 php artisan migrate --path=/database/migrations/2017_08_29_130700_add_colum_user_is_black_list.php

I also run the following command

php artisan migrate

But when I run the above command then I receive following error

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists

Upvotes: 3

Views: 1101

Answers (1)

RAUSHAN KUMAR
RAUSHAN KUMAR

Reputation: 6006

You need to follow the following steps for adding a column in existing table using laravel migrations.

Laravel 3+, 4+

  1. Make a migration using CLI as (Use a specific name to avoid clashing with existing models)

    php artisan migrate:make add_dob_users_table --table=users

Laravel 5+

  1. Make a migration using CLI as (Use a specific name to avoid clashing with existing models)

    php artisan make:migration add_dob_to_users

Laravel 3+

  1. Use the Schema::table() method (As you're accessing an existing table, not creating a new one) and add a column like this:

    public function up()
    {
        Schema::table('users', function($table) {
            $table->string('dob')->nullable()->change();
        });
    }
    
  2. Add the rollback option:

    public function down()
    {
        Schema::table('users', function($table) {
            $table->dropColumn('dob');
        });
    }
    

Laravel 4+, 5+

  1. Use the Schema::table() method (As you're accessing an existing table, not creating a new one) and add a column like this:

    public function up()
    {
        Schema::table('users', function(Blueprint $table) {
            $table->string('dob')->nullable()->change();
        });
    }
    
  2. Add the rollback option:

    public function down()
    {
        Schema::table('users', function(Blueprint $table) {
            $table->dropColumn('dob');
        });
    }
    

Laravel 3+, 4+, 5+

  1. Finally run your migrations as

    php artisan migrate

Docs for creating and running migrations are

Laravel 3+

Laravel 3 Schema Builder

Laravel 3 Migrations

Laravel 4, 5

Laravel Schema Builder

Laravel Migrations

Upvotes: 2

Related Questions