user0129e021939232
user0129e021939232

Reputation: 6355

Laravel migrate database database using artisan with new columns

I am using laravel and I am trying to do another migration but the table already exists so it is throwing the following error:

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

I've added this line into the migration file:

$table->softDeletes();

This is the full file:

<?php

use Illuminate\Database\Migrations\Migration;

class ConfideSetupUsersTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up()
    {
        // Creates the users table
        Schema::create('users', function ($table) {
            $table->increments('id');
            $table->string('firstname');
            $table->string('lastname');
            $table->string('username')->unique();
            $table->string('email')->unique();
            $table->string('password');
            $table->string('confirmation_code');
            $table->string('remember_token')->nullable();
            $table->softDeletes();
            $table->boolean('confirmed')->default(false);
            $table->timestamps();
        });

        // Creates password reminders table
        Schema::create('password_reminders', function ($table) {
            $table->string('email');
            $table->string('token');
            $table->timestamp('created_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down()
    {
        Schema::drop('password_reminders');
        Schema::drop('users');
    }
}

Any ideas what I'm doing wrong?

Upvotes: 1

Views: 1044

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152860

You can rollback the migration and then run it again:

php artisan migrate:rollback

And then:

php artisan migrate

Attention in your case this will delete all data from the table since it will drop users and password reminder and then recreate it.

The alternative is to create a new migration:

php artisan migrate:make users_add_soft_deletes

Put this in there:

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

And run php artisan migrate to apply the new migration

Upvotes: 2

Related Questions