LaraveldeにてMigrationするときのデータ型などの指定方法についてまとめておきます。
基本的には、ちゃんとしたドキュメントを見るべきでしょうが、本当によく使うものを列挙しています。
https://laravel.com/docs/12.x/migrations
1.1 数値データ型
メソッド | 説明 | 例 |
---|---|---|
integer | INTEGER 同等の列を作成します。 | $table->integer(‘votes’); |
id | bigIncrements のエイリアス。自動増分 UNSIGNED BIGINT (主キー) の同等の列を作成する。 | $table->id(); |
smallInteger | SMALLINT 同等の列を作成します。 | $table->smallInteger(‘votes’); |
bigInteger | BIGINT 同等の列を作成します。 | $table->bigInteger(‘votes’); |
double | DOUBLE 同等の列を作成します。 | $table->double(‘amount’); |
decimal | DECIMAL 指定された精度 (合計桁数) とスケール (小数点以下の桁数) を持つ同等の列を作成します。 | $table->decimal(‘amount’, total: 8, places: 2); |
1.2 文字列データ型
メソッド | 説明 | 型 |
---|---|---|
char | CHAR 指定された長さの同等の列を作成します。 | $table->char(‘name’, length: 100); |
string | VARCHAR 指定された長さの同等の列を作成します。 | $table->string(‘name’, length: 100); |
text | TEXT 同等の列を作成します。 | $table->text(‘description’); |
longText | LONGTEXT 同等の列を作成します。 | $table->longText(‘description’); |
1.3 日付データ型
$table->timestamp('updated_dt')->useCurrent()->useCurrentOnUpdate()->nullable(false);
$table->timestamp('created_dt')->useCurrent()->nullable(false);
メソッド | 説明 | 型 |
---|---|---|
dateTime | DATETIME オプションで小数秒の精度を持つ同等の列を作成します。 | $table->dateTime(‘created_at’, precision: 0); |
date | DATE 同等の列を作成します。 | $table->date(‘created_at’); |
2.1 インデックス・制約
unique
列定義
$table->string('email')->unique();
もしくは、
$table->unique('email');
index
$table->index('state');