skip to Main Content

i want to add new enum values called "pending" to existing table so the enum will be [‘paid’, ‘unpaid’, ‘pending’, ‘free’]

the table on phpmyadmin

so i try to define it on migration and i think it would automatically update when i run "php artisan migrate" but there is no migrate

migration file

So can you help me how to add the new values "pending"?

2

Answers


  1. You migration file should be like this

    public function up(): void
    {
        Schema::table('payment_spps', function (Blueprint $table) {
            $table->enum('January', ['paid', 'unpaid', 'pending', 'free',])->default('unpaid')->change();
            // other columns ...
        });
    }
    

    If it dose not work , you can try this

    public function up(): void
    {
        Schema::table('payment_spps', function (Blueprint $table) {
            DB::statement("ALTER TABLE payment_spps MODIFY COLUMN `January` ENUM('paid', 'unpaid', 'pending', 'free')");
        });
    }
    
    
    

    if you want to set default value with it

    public function up(): void
    {
        Schema::table('payment_spps', function (Blueprint $table) {
            DB::statement("ALTER TABLE payment_spps MODIFY COLUMN `January` ENUM('paid', 'unpaid', 'pending', 'free') default('unpaid') ");
        });
    }
    
    
    Login or Signup to reply.
  2. Create a New Migration:
    Run the following Artisan command to create a new migration file:

    php artisan make:migration add_pending_to_status_enum_in_table_name --table=your_table_name
    

    Here’s an example of what the up and down methods in your migration file might look like:

    <?php
    
    use IlluminateDatabaseMigrationsMigration;
    use IlluminateDatabaseSchemaBlueprint;
    use IlluminateSupportFacadesSchema;
    use IlluminateSupportFacadesDB;
    
    class AddPendingToStatusEnumInTableName extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            DB::statement("ALTER TABLE your_table_name MODIFY COLUMN your_enum_column ENUM('paid', 'unpaid', 'pending', 'free')");
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            DB::statement("ALTER TABLE your_table_name MODIFY COLUMN your_enum_column ENUM('paid', 'unpaid', 'free')");
        }
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search