1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[中学生でもわかるLaravel]カラム基礎記述<2025>

Last updated at Posted at 2025-06-19

今回やること

  • Laravelのカラム作成時に頻出する記述をまとめる

0.初めに

 データベース難しいのでまずはミクロの記述から学ぼうという試みです。
マイグレーションやシード全体の事もいつか記事にしたい。

よく使うもの、私が開発中にお世話になったものをまとめています。

💡筆者もまだ勉強中なので、内容は7割くらいの信頼度で読んでください。
またコメントしていただけると、私の大きな学びになるので是非お願いします。

1.カラムのデータ型

2024_05_04_093730_create_posts_table.php
//マイグレーション記述例
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->integer('id')//id
            $table->unsignedBigInteger('user_id'); //ユーザーid
            $table->string('post', 400);//投稿内容
            $table->timestamp('created_at')//作った時間
            $table->timestamp('updated_at')//最終編集した時間
        });
    }

分類 メソッド記法 説明
テキスト string() 文字列(255文字まで)
テキスト text() 長文テキスト(制限なし)
id id() increments('id') のエイリアス。主キー(自動増分)
id bigIncrements() 大きな自動増分主キー。主に id() の代替
integer() 整数(符号付き)
時間 timestamps() created_atupdated_at を自動で追加
時間 date() 年月日(例: 2025-06-19)
時間 dateTime() 年月日時分秒(例: 2025-06-19 14:00:00)
リレーション foreignId('user_id')->constrained() users.id に外部キー制約
リレーション unsignedBigInteger('user_id') 外部キーの手動定義に使う(bigIncrements対応)
リレーション foreignIdFor(User::class) モデルから自動で外部キーを生成(Laravel 7+)
bool boolean() true / false フラグ
その他 softDeletes() deleted_at カラムを追加
その他 json('options') JSON形式でデータ保存
その他 rememberToken() Laravelログイン機能用のトークン
その他 enum('status', ['draft', 'published']) 限定された値のセット

2.カラムの修飾子

2024_05_04_093730_create_posts_table.php
//マイグレーション記述例
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->integer('id')->autoIncrement();//自動増分
            $table->unsignedBigInteger('user_id'); // user_idをunsignedBigIntegerに変更
            $table->string('post', 400);
            $table->timestamp('created_at')->useCurrent();//現在の時間に合わせる
            $table->timestamp('updated_at')->default(DB::raw('current_timestamp on update current_timestamp'));
            // 外部キー制約を追加
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

分類 メソッド記法 説明
id ->unsigned() id などに負の値を許さないとき
id ->autoIncrement() 自動増分(integer 型やカスタム主キーに使う)
id ->primary() 主キーになるよう指定
テキスト ->default($value) 初期値を指定したいとき
時間 ->useCurrent() created_at など日付系カラムの初期値を現在時刻に設定
時間 ->useCurrentOnUpdate() updated_at など更新時に現在時刻を自動設定
その他 ->comment('...') カラムの説明を付けておきたいとき
その他 ->nullable() 値の省略を許可したいとき(NULL可)

終わりに

お疲れ様でした。

データベースってホントに難解ですよね。流れはつかめても記述が難しく、記述を追うと流れが見えづらくなる。整理して勉強していこうと思います。

もし誰かの役にたてたなら幸いです。
またコメント待っていますので、「みたよ」だけでもいいので是非よろしくお願いします。

追記

マイグレーション作成のコマンド

php artisan make:migration create_{テーブル名}_table

マイグレーションじっこう

php artisan migrate
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?