0
0

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 PHP】マイグレーションファイルの中身を深掘り

0
Last updated at Posted at 2026-05-03

今回はLaravel環境下でマイグレーションファイルを作成した際の
中身の深掘りをしていきます。

環境
Laravel13
PHP 8.4
Docker Sail使用

☆この記事でわかること☆

①Laravelのマイグレーションファイルの中身を理解できる
②要素をどこに追加すれば良いか理解できる
③なぜその記述をする必要があるか理解できる

マイグレーションファイルを作成

今回は
とあるアプリの「物語のジャンル」に関しての処理をするため、
「genres_table」という名前のマイグレーションファイルを作ります。

# マイグレーションファイルの作成
./vendor/bin/sail artisan make:migration create_genres_table

# こんなファイルができる
database/migrations/d2026_05_03_081607_create_genres_table

中身を紐解いていきます

上から少しずつ分解して理解を深めていきます。

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('genres', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('genres');
    }
};

1:マイグレーションファイルを便利に使うために必須のコマンド


# Migrationクラスを使う宣言、これがあることでup() down()が使える(後述)
use Illuminate\Database\Migrations\Migration;

# テーブルの設計図クラス(青地図) これのおかげで簡単な記述法
# $table->string('name')みたいな書き方ができる
use Illuminate\Database\Schema\Blueprint;

# DBのテーブルを操作するためのクラス テーブルを『作る』『消す』という命令を出せる
use Illuminate\Support\Facades\Schema;

2:処理の大枠を動かすために必要な new class

# Migrationを継承した無名クラスを その場で作ってLaravelに渡す
return new class extends Migration

return はこのファイルを読み込んだ際に、
この クラスのインスタンスを返す という意味。

new class というのは 「名前のないクラス」 のこと。
マイグレーションファイルは 一回しか生成されないので 名前を付ける必要がない
基本的にはマイグレーションファイルの作成時に自動生成される

extends Migration というのは
Migrationクラスを継承しているという意味で後述のup() down()を使うために必要

3: 2のクラスの中身を深掘り 主な処理をここに書く

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('genres', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('genres');
    }
};
テーブル作成時の処理
public function up(): void

migrateしたときに実行される処理
「テーブルを作る」処理をここに書く。

Schema::create('genres', function (Blueprint $table) {

genresという名前のテーブルを作る宣言
$tableという設計図に対してカラムを追加する。

$table->id();

自動連番のidカラムを追加
DBの主キーに当たる場所

$table->string('name');

nameという名前のVARCHAR(255)カラムを追加
この場合は255文字以内のテキストを保存できる

$table->timestamps();

created_atupdated_atの2カラムを自動追加
作成日時・更新日時が自動で記録される

public function down(): void

migrate:rollbackしたときに実行される処理
「テーブルを消す」処理をここに書く。
up()の逆をする。

up() down()の記述についてより詳しく

簡単にまとめると以下の通りです。
up() = 進む (テーブルを作る)
down() = 戻る (テーブルを消す)

これを記述できるルールにしておくと

# up()が実行される
./vendor/bin/sail artisan migrate
→ genresテーブルが作られる

# down()が実行される
./vendor/bin/sail artisan migrate:rollback
→ genresテーブルが消える

upはわかるが、なぜdownもあるのか

テーブルを作成した後にカラムが違うことに気付いた際

→ migrate:rollback でテーブルを消して
→ マイグレーションを修正して
→ もう一度 migrate する
こういった処理を行うことができる

up downのようにLaravelはコードを明示的に書く必要がある

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?