LoginSignup
3
4

More than 5 years have passed since last update.

【Laravel5.4】migration機能を用いたテーブルのカラム名変更方法

Last updated at Posted at 2018-05-26

まずマイグレーションの生成を行う。
この時、Artisanコマンド「make:migration」を使って作ります。

例)生年月日のカラム名をbirthからbirthdateに変更する場合
オプション:--table [table name]

vagrant@vm:~/app$ php artisan make:migration rename_birth_to_birthdate_on_contacts_table --table=contacts

このコマンドを実行すると、database/migrations以下に生成日時がついたマイグレーションが作られます。
以下生成されたファイル内容

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

class RenameBirthToBirthdateOnContactsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('contacts', function (Blueprint $table) {
          //
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('contacts', function (Blueprint $table) {
          //
        });
    }
}



このファイルに対して、upメソッドに変更する内容downメソッドに変更した内容の打消し処理(ロールバック)を記述します。

例)生年月日のカラム名をbirthからbirthdateに変更する場合

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

class RenameBirthToBirthdateOnContactsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('contacts', function (Blueprint $table) {
            $table->renameColumn('birth', 'birth_date');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('contacts', function (Blueprint $table) {
            $table->renameColumn('birth_date', 'birth');
        });
    }
}

処理を各メソッドに記述し終わったら、再度Artisanコマンドを叩いて実際にDBに対して処理を実行します。

vagrant@vm:~/app$ php artisan migrate

あとはDBの構造を確認し、変更が反映されていることを確認しておく。
生成したマイグレーションはgitで管理しておく。
※その際に発生したDB起因の変更は修正してコミットする。

以下Artisanコマンド「make:migration」のオプション一覧

vagrant@vm:~/app$ php artisan make:migration --help
Usage:
  make:migration [options] [--] <name>

Arguments:
  name                   The name of the migration.

Options:
      --create[=CREATE]  The table to be created.
      --table[=TABLE]    The table to migrate.
      --path[=PATH]      The location where the migration file should be created.
  -h, --help             Display this help message
  -q, --quiet            Do not output any message
  -V, --version          Display this application version
      --ansi             Force ANSI output
      --no-ansi          Disable ANSI output
  -n, --no-interaction   Do not ask any interactive question
      --env[=ENV]        The environment the command should run under
  -v|vv|vvv, --verbose   Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
  Create a new migration file

おわり

  • 今更感がすごいんですが下書きにのこってたので投稿しますね

参考

3
4
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
3
4