sourcecode

Larvel 마이그레이션을 통해 열의 기본값 변경

copyscript 2023. 10. 1. 21:53
반응형

Larvel 마이그레이션을 통해 열의 기본값 변경

기본값이 이미 할당된 테이블이 있습니다.예를 들어 다음을 살펴볼 수 있습니다.

Schema::create('users', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->integer('active')->default(1);
        });

이제 활성 필드의 기본값을 변경합니다.저는 다음과 같은 일을 할 것으로 기대하고 있습니다.

if (Schema::hasTable('users')) {
        Schema::table('users', function (Blueprint $table) {
            if (Schema::hasColumn('users', 'active')) {
                $table->integer('active')->default(0);
            }
        });
    }

하지만 물론 칼럼은 이미 거기에 있다는 것을 말해줍니다.어떻게 하면 열을 떨어뜨리지 않고 x열의 기본값을 간단히 업데이트할 수 있습니까?

메소드를 사용할 수 있습니다.

Schema::table('users', function ($table) {
    $table->integer('active')->default(0)->change();
});

그다음달리기migrate지휘.

갱신하다

Laravel 4의 경우 다음과 같은 것을 사용합니다.

DB::statement('ALTER TABLE `users` CHANGE COLUMN `active` `active` INTEGER NOT NULL DEFAULT 0;');

안에서.up()대신 방법Schema::table();

열을 업데이트하려면 변경 함수를 호출해야 합니다.

if (Schema::hasTable('users')) {
    Schema::table('users', function (Blueprint $table) {
        if (Schema::hasColumn('users', 'active')) {
            $table->integer('active')->default(0)->change();
        }
    });
}

새 마이그레이션 파일을 만들어 사용합니다.change()

if (Schema::hasTable('users')) {
    Schema::table('users', function (Blueprint $table) {
        if (Schema::hasColumn('users', 'active')) {
            $table->integer('active')->default(0)->change();
        }
    });
}

또한 추가해야 합니다.doctrine/dbalcomposer.json 파일 종속성

언급URL : https://stackoverflow.com/questions/37003469/laravel-migrations-change-default-value-of-column

반응형