Laravel 프레임워크를 사용하여 기존 데이터베이스 테이블에 새 열을 추가하는 방법을 찾을 수 없습니다.
다음을 사용하여 마이그레이션 파일을 편집하려고 했습니다.
<?php public function up() { Schema::create('users', function ($table) { $table->integer("paid"); }); }
단말기에서, 나는php artisan migrate:install
그리고.migrate
.
새 열을 추가하려면 어떻게 해야 합니까?
질문에 대한 답변
마이그레이션을 작성하려면 Artisan CLI에서 migrate:make 명령을 사용합니다.기존 모델과 충돌하지 않으려면 특정 이름 사용
Larabel 5+의 경우:
php artisan make:migration add_paid_to_users_table --table=users
Larabel 3의 경우:
php artisan migrate:make add_paid_to_users
그 후, 를 사용할 필요가 있습니다.Schema::table()
method(기존 테이블에 액세스할 때 새 테이블을 만들지 않음)다음과 같은 열을 추가할 수 있습니다.
public function up() { Schema::table('users', function($table) { $table->integer('paid'); }); }
롤백 옵션을 추가하는 것을 잊지 마십시오.
public function down() { Schema::table('users', function($table) { $table->dropColumn('paid'); }); }
그런 다음 마이그레이션을 실행할 수 있습니다.
php artisan migrate
이는 모두 Larabel 4 / Larabel 5에 관한 문서에서 잘 설명되어 있습니다.
Larabel 3의 경우:
편집:
사용하다$table->integer('paid')->after('whichever_column');
특정 컬럼 뒤에 이 필드를 추가합니다.
Larabel 5.1 이후를 사용하는 미래의 독자들을 위해 mike3875의 답변을 추가하겠습니다.
작업을 빠르게 진행하려면 다음과 같이 “–table” 플래그를 사용할 수 있습니다.
php artisan make:migration add_paid_to_users --table="users"
이렇게 하면up
그리고.down
메서드 내용 자동:
/** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function (Blueprint $table) { // }); }
마찬가지로, 다음과 같은 방법으로--create["table_name"]
옵션을 선택하면 이행에 보일러 플레이트가 추가됩니다.작은 점이지만 많은 작업을 할 때 도움이 됩니다!
라라벨 5.6 이상
기존 테이블에 FORENAL KEY로 새 열을 추가하려는 경우.
make: migration 명령을 실행하여 새 마이그레이션을 만듭니다.
예:
php artisan make:migration add_store_id_to_users_table --table=users
데이터베이스/이행 폴더에 다음과 같은 새 마이그레이션 파일이 있습니다.
2018_08_093431_add_store_id_to_users_table.vp (댓글 참조)
<?php use IlluminateSupportFacadesSchema; use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; class AddStoreIdToUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function (Blueprint $table) { // 1. Create new column // You probably want to make the new column nullable $table->integer('store_id')->unsigned()->nullable()->after('password'); // 2. Create foreign key constraints $table->foreign('store_id')->references('id')->on('stores')->onDelete('SET NULL'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function (Blueprint $table) { // 1. Drop foreign key constraints $table->dropForeign(['store_id']); // 2. Drop the column $table->dropColumn('store_id'); }); } }
그런 다음 다음 명령을 실행합니다.
php artisan migrate
어떤 이유로든 마지막 이행을 취소하려면 다음 명령을 수행합니다.
php artisan migrate:rollback
마이그레이션에 대한 자세한 내용은 문서를 참조하십시오.
Larabel 5를 사용하는 경우 명령어는 다음과 같습니다.
php artisan make:migration add_paid_to_users
물건(컨트롤러, 모델, 이행 등)을 만들기 위한 모든 명령어가make:
명령어를 입력합니다.
php artisan migrate
그래도 똑같아요.
인 라라벨 8
php artisan make:migration add_columnname_to_tablename_table --table=tablename
다음으로 이행 작성 후
public function up() { Schema::table('users', function (Blueprint $table) { // 1. Create new column $table->datatype('column_name')->nullable(); }); } public function down() { Schema::table('users', function (Blueprint $table) { // 1. Create new column $table->dropColumn('column_name'); }); }
그 후 실행
php artisan migrate
오류가 발생할 경우 마이그레이션 이름을 테이블 작성 전 날짜로 변경한 후 다시 실행합니다.php artisan migrate