Laravel マイグレーションファイルの記載方法


目的

  • 毎回一筋縄ではいかないためいい加減覚えるためにまとめる

実施環境

  • ハードウェア環境
項目 情報
OS macOS Catalina(10.15.3)
ハードウェア MacBook Pro (16-inch ,2019)
プロセッサ 2.6 GHz 6コアIntel Core i7
メモリ 16 GB 2667 MHz DDR4
グラフィックス AMD Radeon Pro 5300M 4 GB Intel UHD Graphics 630 1536 MB
  • ソフトウェア環境
項目 情報 備考
PHP バージョン 7.4.3 Homwbrewを用いて導入
Laravel バージョン 7.0.8 commposerを用いて導入
MySQLバージョン 8.0.19 for osx10.13 on x86_64 Homwbrewを用いて導入

前提情報

記載例

  • 例として下記コマンドを用いて作成したマイグレーションファイルを開く。

    $ php artisan make:migration add_flag_column_to_users_table --table=users
    
  • マイグレーションファイルは下記のように記載されている。

    アプリ名ディレクトリ/databases/migrations/YYYY_MM_DD_XXXXXX_add_flag_column_to_users_table.php
    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class AddFlagColumnToUsersTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('users', function (Blueprint $table) {
                //
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::table('users', function (Blueprint $table) {
                //
            });
        }
    }
    
  • カラムを追加、もしくは新規作成したい場合はpublic function up()に追加するカラム情報を記載する。

  • 既存カラムを削除したい場合はpublic function down()に追加するカラム情報を記載する。

  • カラムを追加する際の追加カラム情報の記載例を下記に記載する。

    アプリ名ディレクトリ/databases/migrations/YYYY_MM_DD_XXXXXX_add_flag_column_to_users_table.php
    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class AddFlagColumnToUsersTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('users', function (Blueprint $table) {
                $table->追加カラムのデータ型('追加カラム名')
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::table('users', function (Blueprint $table) {
                //
            });
        }
    }
    

具体例

  • 既存の「users」テーブルに「flag」カラムをデータ型「integer」で追加するマイグレーションファイルの書き方の例を下記に記載する。

    アプリ名ディレクトリ/databases/migrations/YYYY_MM_DD_XXXXXX_add_flag_column_to_users_table.php
    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class AddFlagColumnToUsersTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('users', function (Blueprint $table) {
                $table->integer('flag')
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::table('users', function (Blueprint $table) {
                //
            });
        }
    }
    
  • 既存の「users」テーブルに「flag」カラムをデータ型「integer」指定がない場合初期値として「0」を格納するものを追加するマイグレーションファイルの書き方の例を下記に記載する。

    アプリ名ディレクトリ/databases/migrations/YYYY_MM_DD_XXXXXX_add_flag_column_to_users_table.php
    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class AddFlagColumnToUsersTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('users', function (Blueprint $table) {
                $table->integer('flag')->default(0)
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::table('users', function (Blueprint $table) {
                //
            });
        }
    }
    
  • 既存の「users」テーブルに「memo」カラムを文字数100文字制限のデータ型「string」で追加するマイグレーションファイルの書き方の例を下記に記載する。

    アプリ名ディレクトリ/databases/migrations/YYYY_MM_DD_XXXXXX_add_flag_column_to_users_table.php
    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class AddFlagColumnToUsersTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('users', function (Blueprint $table) {
                $table->string('memo', 100);
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::table('users', function (Blueprint $table) {
                //
            });
        }
    }