Laravel migration カラム編集まとめ


laravelではmigrationファイルを作成することで簡単にテーブルとかカラムの作成ができますが
テーブル作ってからカラム編集したい...!ってなったときに毎回方法ググるので
よく使うカラムの編集方法とかdown処理とかも含めてまとめ。

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

まずはmigrationファイルの作成。
新規作成の場合は--create、テーブル編集の場合は--tableオプションを後ろにつけると
migrationファイルに指定のテーブル名を含んだ状態で作成してくれる。

#新規作成
php artisan make:migration create_users_table --create=users

#編集(編集内容がわかりやすいファイル名にすると◎)
php artisan make:migration modify_users_table --table=users

テーブル作成

テーブル作成の詳しい説明は割愛。
usersテーブルの中にid,name,email,password,note,hoge
timestampsカラムがある状態でテーブル作成するとします。

2014_10_12_000000_create_users_table.php
<?php

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

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->string('note');
            $table->string('hoge');
            $table->timestamps();
        });
    }

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

テーブル(カラム)編集

こっちが本題。
編集の仕方いろいろ。

2014_10_12_000000_modify_users_table.php
<?php

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

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            #カラム名を変更する
            $table->renameColumn('name', 'user_name');

            #カラムをnull許可に変更する
            $table->string('email')->nullable()->change();

            #カラムのデータ型を変更する
            $table->integer('password')->change();

            #date型カラムをpasswordカラムの後ろに追加する
            $table->date('birth_date')->after('password');

            #noteカラムにindexを貼る
            $table->index('note')->change();

            #hogeカラムをunique値にしてコメントを入れる
            $table->string('hoge')->unique()->comment('ほげ')->change();

        });
    }

    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            #元のカラム名に戻す
            $table->renameColumn('user_name', 'name');

            #nullを許可しないに変更
            $table->string('email')->nullable(false)->change();

            #カラムのデータ型を元のデータ型に変更する
            $table->string('password')->change();

            #追加したカラムを削除する
            $table->dropColumn('birth_date');

            #indexを削除する
            $table->dropIndex('note');

            #uniqueキーを削除してコメントを削除する
            $table->dropUnique('hoge')->comment('')->change();
        });
    }
}

migrationファイルの編集ができたらmigrationを実行

php artisan migrate

結果

元々のテーブル

# カラム名 データ型 null キー コメント
1 id bigint いいえ primary
2 name varchar(255) いいえ
3 email varchar(255) いいえ unique
4 password varchar(255) いいえ
5 note varchar(255) いいえ
6 hoge varchar(255) いいえ
7 created_at timestamp はい
8 updated_at timestamp はい

編集後のテーブル

# カラム名 データ型 null キー コメント
1 id bigint いいえ primary
2 user_name varchar(255) いいえ
3 email varchar(255) はい unique
4 password Int(11) いいえ
5 birth_date date いいえ
6 note varchar(255) いいえ index
7 hoge varchar(255) いいえ unique ほげ
8 created_at timestamp はい
9 updated_at timestamp はい

ちなみにrollbackすると元々のテーブルに戻る。

php artisan migrate:rollback

参考

https://readouble.com/laravel/5.5/ja/migrations.html
https://pgmemo.tokyo/data/archives/1381.html
https://pgmemo.tokyo/data/archives/1545.html