Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel Migrations breaks when migrating new tables into the database

Iam trying to migrate Laravel table migrations into the database but every time I make new tables and run php artisan migrate Laravel complains that user table exists and even the newly added migrations are not created in the database, and when I run php artisan migrate:fresh I lose my data is there a better way to do this without losing my data in the already existing tables. Below is the table i want to add.

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up() {

        Schema::create('drivers', function (Blueprint $table) {
            $table->id();
            $table->string('name')->nullable();
            $table->string('email')->unique();
            $table->string('contact')->unique()->nullable();
            $table->string('code')->unique();
            $table->string('nin')->nullable();
            $table->date('birthday')->nullable();
            $table->string('image')->nullable();
            $table->bigInteger('country_id')->nullable();
            $table->boolean('verified')->nullable()->default(false);
            $table->timestamps();
            $table->foreign('country_id')->references('id')->on('countries');
        });
    
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down() {
    Schema::dropIfExists('drivers');
}
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can add a Schema condition to all your migrations in that if a table already exists in the database is skipped during the migration process. Check this example below

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        if (!Schema::hasTable('drivers')) {
            Schema::create('drivers', function (Blueprint $table) {
                $table->id();
                $table->string('name')->nullable();
                $table->string('email')->unique();
                $table->string('contact')->unique()->nullable();
                $table->string('code')->unique();
                $table->string('nin')->nullable();
                $table->date('birthday')->nullable();
                $table->string('image')->nullable();
                $table->bigInteger('country_id')->nullable();
                $table->boolean('verified')->nullable()->default(false);
                $table->timestamps();
                $table->foreign('country_id')->references('id')->on('countries');
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down() {
        Schema::dropIfExists('drivers');
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading