カテゴリー
Laravel PHP

Laravel Migrationでデータベーステーブルにカラムを追加

カラムを追加する場合

// below command will generate a new migration file
php artisan make:migration add_omit_root_to_chords --table="chords"

// inside the migration file, write as below to add new columns

public function up()
{
        Schema::table('chords', function (Blueprint $table) {
            $table->boolean("omit_root")->nullable();
            $table->integer("genre")->nullable();
        });
    }
    //  ~~~~~~~~
}

カラムのタイプを変更する場合

// you need doctrine/dbal
composer require doctrine/dbal

// to create migration 
php artisan make:migration change_omit_root_type --table="chords"

// inside migration
Schema::table('chords', function (Blueprint $table) {
    $table->integer("omit_root")->change();
});