Reputation: 896
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
Reputation: 6006
You need to follow the following steps for adding a column in existing table using laravel migrations.
Laravel 3+, 4+
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+
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+
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();
});
}
Add the rollback option:
public function down()
{
Schema::table('users', function($table) {
$table->dropColumn('dob');
});
}
Laravel 4+, 5+
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();
});
}
Add the rollback option:
public function down()
{
Schema::table('users', function(Blueprint $table) {
$table->dropColumn('dob');
});
}
Laravel 3+, 4+, 5+
Finally run your migrations as
php artisan migrate
Docs for creating and running migrations are
Laravel 3+
Laravel 4, 5
Upvotes: 2