Home / プログラミング / 【Laravel】Migrationのチートシート

【Laravel】Migrationのチートシート

LaraveldeにてMigrationするときのデータ型などの指定方法についてまとめておきます。

基本的には、ちゃんとしたドキュメントを見るべきでしょうが、本当によく使うものを列挙しています。

https://laravel.com/docs/12.x/migrations

1.1 数値データ型

メソッド説明
integerINTEGER同等の列を作成します。$table->integer(‘votes’);
idbigIncrements のエイリアス。
自動増分
UNSIGNED BIGINT(主キー) の同等の列を作成する。
$table->id();
smallIntegerSMALLINT同等の列を作成します。$table->smallInteger(‘votes’);
bigIntegerBIGINT同等の列を作成します。$table->bigInteger(‘votes’);
doubleDOUBLE同等の列を作成します。$table->double(‘amount’);
decimalDECIMAL指定された精度 (合計桁数) とスケール (小数点以下の桁数) を持つ同等の列を作成します。$table->decimal(‘amount’, total: 8, places: 2);

1.2 文字列データ型

メソッド説明
charCHAR指定された長さの同等の列を作成します。$table->char(‘name’, length: 100);
stringVARCHAR指定された長さの同等の列を作成します。$table->string(‘name’, length: 100);
textTEXT同等の列を作成します。$table->text(‘description’);
longTextLONGTEXT同等の列を作成します。$table->longText(‘description’);

1.3 日付データ型

$table->timestamp('updated_dt')->useCurrent()->useCurrentOnUpdate()->nullable(false);
$table->timestamp('created_dt')->useCurrent()->nullable(false);
メソッド説明
dateTimeDATETIMEオプションで小数秒の精度を持つ同等の列を作成します。$table->dateTime(‘created_at’, precision: 0);
dateDATE同等の列を作成します。$table->date(‘created_at’);

2.1 インデックス・制約

unique列定義

 $table->string('email')->unique();

もしくは、

$table->unique('email');

index

$table->index('state');

タグ付け処理あり: