skip to Main Content

I make changes in the migration files – specifically, I deleted one field and added a new one, I do a migration (php artisan migrate) but nothing works.

2

Answers


  1. If you have data and you don’t need to lose it, you can make a new migration file that deletes and add column let’s assume that we need to delete the email_verified_at column and add the mobile_verified_at column.

    • first create the migration file
      php artisan make:migration drop_email_verified_at_and_add_mobile_verified_at_to_users_table

    This will be the output of the previous command:

    <?php
    
    use IlluminateDatabaseMigrationsMigration;
    use IlluminateDatabaseSchemaBlueprint;
    use IlluminateSupportFacadesSchema;
    
    return new class extends Migration
    {
        /**
         * Run the migrations.
         */
        public function up(): void
        {
            Schema::table('users', function (Blueprint $table) {
                //
            });
        }
    
        /**
         * Reverse the migrations.
         */
        public function down(): void
        {
            Schema::table('users', function (Blueprint $table) {
                //
            });
        }
    };
    
    • delete the email_verified_at $table->dropColumn('email_verified_at');
    • to add the mobile_verified_at $table->datetime('mobile_verified_at')->nullable();

    The migration file code will be like this

    <?php
    
    use IlluminateDatabaseMigrationsMigration;
    use IlluminateDatabaseSchemaBlueprint;
    use IlluminateSupportFacadesSchema;
    
    return new class extends Migration
    {
        /**
         * Run the migrations.
         */
        public function up(): void
        {
            Schema::table('users', function (Blueprint $table) {
                $table->dropColumn('email_verified_at');
                $table->datetime('mobile_verified_at')->nullable();
            });
        }
    
        /**
         * Reverse the migrations.
         */
        public function down(): void
        {
            Schema::table('users', function (Blueprint $table) {
                //
            });
        }
    };
    
    Login or Signup to reply.
  2. You can either refresh whole table or what I do in these cases is go to the migrations table, find the migration name for the table you have done some changes and remove it from the migrations table. Then go to you code editor and try to migrate.

    Other way – smarter one is, lets say you have posts table and you want to add image fields to that table:

    php artisan make:migration add_image_to_posts_table
    

    Go to newly created migration and migrate it.

    Also, in this case, if you want only to migrate newly created table you can migrate it like this:

    php artisan migrate --path=database/migrations/newly_migration_file_name.php
    

    Voila 🙂 Enjoy

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search