Laravelのマイグレーションでカラムのを追加するには、
今既にあるマイグレーションファイルは 変更せずに置いておいて、変更を記述したマイグレーションファイルを新規に作成します。
テーブル名 | カラム名 | 型 |
---|---|---|
catalogs | start_date | datetime |
↓ (例)こちらの2カラムを追加するとします。
テーブル名 | カラム名 | 型 |
---|---|---|
catalogs | start_date | datetime |
catalogs | start_dat_2 | datetime |
catalogs | start_dat_3 | datetime |
マイグレーションファイル名はなんでもいいですが、クラス名(1番目の引数)が被ってしまうとエラーになるので、注意して命名してください。
php artisan make:migration change_catalogs_table_add_2columns --table=catalogs
クラス名(class ChangeConsentformsDelBiko extends Migration)でファイルが作成されます。
成功すると 次のようなファイルが生成されます
2019_07_08_180737_change_catalogs_table_add_2columns
次のように記述します。
(複数カラムを追加する時は追加する順番に注意しましょう)
after('start_date') で start_date カラムの後ろに追加しています。
before() メソッドはうまく動作しないことがあるので、after を使いましょう。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeCatalogsTableAdd2columns extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('catalogs', function (Blueprint $table) {
$table->datetime('start_date_3')->nullable()->after('start_date')->comment('開始時間_3(開始日時_3)');
});
Schema::table('catalogs', function (Blueprint $table) {
$table->datetime('start_date_2')->nullable()->after('start_date')->comment('開始時間_2(開始日時_2)');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('catalogs', function (Blueprint $table) {
$table->dropColumn('start_date_3');
});
Schema::table('catalogs', function (Blueprint $table) {
$table->dropColumn('start_date_2');
});
}
}
コマンド | 説明 |
---|---|
$table->bigIncrements('id'); |
符号なしBIGINTを使用した自動増分ID(主キー) |
$table->bigInteger('votes'); |
BIGINTカラム |
$table->binary('data'); |
BLOBカラム |
$table->boolean('confirmed'); |
BOOLEANカラム |
$table->char('name', 100); |
オプションの文字長を指定するCHARカラム |
$table->date('created_at'); |
DATEカラム |
$table->dateTime('created_at'); |
DATETIMEカラム |
$table->dateTimeTz('created_at'); |
タイムゾーン付きDATETIMEカラム |
$table->decimal('amount', 8, 2); |
有効(全体桁数)/小数点以下桁数指定のDECIMALカラム |
$table->double('amount', 8, 2); |
有効(全体桁数)/小数点以下桁数指定のDOUBLEカラム |
$table->enum('level', ['easy', 'hard']); |
ENUMカラム |
$table->float('amount', 8, 2); |
有効(全体桁数)/小数点以下桁数指定のFLOATカラム |
$table->geometry('positions'); |
GEOMETRYカラム |
$table->geometryCollection('positions'); |
GEOMETRYCOLLECTIONカラム |
$table->increments('id'); |
符号なしINTを使用した自動増分ID(主キー) |
$table->integer('votes'); |
INTEGERカラム |
$table->ipAddress('visitor'); |
IPアドレスカラム |
$table->json('options'); |
JSONフィールド |
$table->jsonb('options'); |
JSONBフィールド |
$table->lineString('positions'); |
LINESTRINGカラム |
$table->longText('description'); |
LONGTEXTカラム |
$table->macAddress('device'); |
MACアドレスカラム |
$table->mediumIncrements('id'); |
符号なしMEDIUMINTを使用した自動増分ID(主キー) |
$table->mediumInteger('votes'); |
MEDIUMINTカラム |
$table->mediumText('description'); |
MEDIUMTEXTカラム |
$table->morphs('taggable'); |
符号なしINTERGERのtaggable_id と文字列のtaggable_type を追加 |
$table->multiLineString('positions'); |
MULTILINESTRINGカラム |
$table->multiPoint('positions'); |
MULTIPOINTカラム |
$table->multiPolygon('positions'); |
MULTIPOLYGONカラム |
$table->nullableMorphs('taggable'); |
NULL値可能なmorphs() カラム |
$table->nullableTimestamps(); |
timestamps() メソッドの別名 |
$table->point('position'); |
POINTカラム |
$table->polygon('positions'); |
POLYGONカラム |
$table->rememberToken(); |
VARCHAR(100)でNULL値可能なremember_token を追加 |
$table->smallIncrements('id'); |
符号なしSMALLINTを使用した自動増分ID(主キー) |
$table->smallInteger('votes'); |
SMALLINTカラム |
$table->softDeletes(); |
ソフトデリートのためにNULL値可能なdeleted_at TIMESTAMPカラム追加 |
$table->softDeletesTz(); |
ソフトデリートのためにNULL値可能なdeleted_at タイムゾーン付きTIMESTAMPカラム追加 |
$table->string('name', 100); |
オプションの文字長を指定したVARCHARカラム |
$table->text('description'); |
TEXTカラム |
$table->time('sunrise'); |
TIMEカラム |
$table->timeTz('sunrise'); |
タイムゾーン付きTIMEカラム |
$table->timestamp('added_on'); |
TIMESTAMPカラム |
$table->timestampTz('added_on'); |
タイムゾーン付きTIMESTAMPカラム |
$table->timestamps(); |
NULL値可能なcreated_at とupdated_at カラム追加 |
$table->timestampsTz(); |
タイムゾーン付きのNULL値可能なcreated_at とupdated_at カラム追加 |
$table->tinyIncrements('id'); |
符号なしTINYINTを使用した自動増分ID(主キー) |
$table->tinyInteger('votes'); |
TINYINTカラム |
$table->unsignedBigInteger('votes'); |
符号なしBIGINTカラム |
$table->unsignedDecimal('amount', 8, 2); |
有効(全体桁数)/小数点以下桁数指定の符号なしDECIMALカラム |
$table->unsignedInteger('votes'); |
符号なしINTカラム |
$table->unsignedMediumInteger('votes'); |
符号なしMEDIUMINTカラム |
$table->unsignedSmallInteger('votes'); |
符号なしSMALLINTカラム |
$table->unsignedTinyInteger('votes'); |
符号なしTINYINTカラム |
$table->uuid('id'); |
UUIDカラム |
$table->year('birth_year'); |
YEARカラム |
php artisan migrate
php artisan migrate:rollback
ロールバックする時にDBカラムが存在しないとエラーとなります。
そこで存在チェックを入れましょう
$table->dropColumn('text_name');
↓ ( newsテーブルに text_name カラムが存在するなら削除する)
if (Schema::hasColumn('news', 'text_name')){
$table->dropColumn('text_name');
}