php 如何在表中添加新的ENUM值

kmb7vmvb  于 2023-11-16  发布在  PHP
关注(0)|答案(2)|浏览(98)

我想添加新的枚举值称为“挂起”到现有的表,使枚举将被删除“已付”,“未付”,“挂起”,“免费”]
the table on phpmyadmin
所以我尝试在迁移时定义它,我认为当我运行“php artisan migrate”时它会自动更新,但没有迁移
migration file
那么你能帮助我如何添加新的值“pending”吗?

sbtkgmzw

sbtkgmzw1#

创建新移植:运行以下Artisan命令以创建新移植文件:

php artisan make:migration add_pending_to_status_enum_in_table_name --table=your_table_name

字符串
下面是迁移文件中的up和down方法的示例:

<?php

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

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')");
    }
}

h5qlskok

h5qlskok2#

你的迁移文件应该是这样的

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

字符串
如果不行,你可以试试这个

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')");
    });
}


如果要使用它设置默认值

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') ");
    });
}

相关问题