1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Laravel】バージョン8.27から利用可能なafterメソッドでカラムを追加する

Posted at

はじめに

Laravel8.27よりafterメソッドで既存のカラムのあとに複数のカラムを**追加できるようになりました。この記事では、afterメソッドでカラムを追加する方法についてまとめました。

前提

追加するテーブル構造

今回Userテーブルのカラム「name」のあとに新しくカラム「birth_year、birth_month、birth_day」を追加していきます。

+-------------------+-----------------+------+-----+---------+----------------+
| Field             | Type            | Null | Key | Default | Extra          |
+-------------------+-----------------+------+-----+---------+----------------+
| id                | bigint unsigned | NO   | PRI | NULL    | auto_increment |
| name              | varchar(255)    | NO   |     | NULL    |                |
| email             | varchar(255)    | NO   | UNI | NULL    |                |
| email_verified_at | timestamp       | YES  |     | NULL    |                |
| password          | varchar(255)    | NO   |     | NULL    |                |
| remember_token    | varchar(100)    | YES  |     | NULL    |                |
| created_at        | timestamp       | YES  |     | NULL    |                |
| updated_at        | timestamp       | YES  |     | NULL    |                |
+-------------------+-----------------+------+-----+---------+----------------

migrationファイルを追加する

$ php artisan make:migration add_birth_to_users_table --table=users

afterメソッドを使用してカラム追加

public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->after('name', function ($table) {
                $table->integer('birth_year');
                $table->integer('birth_month');
                $table->integer('birth_day');
            });
        });
    }

修正が終わったら、マイグレーション実行する。

$ php artisan migrate
Migrating: 2021_02_20_142718_add_birth_to_users_table
Migrated:  2021_02_20_142718_add_birth_to_users_table (342.99ms)

きちんとカラム追加されているか確認。

mysql> desc users;
+-------------------+-----------------+------+-----+---------+----------------+
| Field             | Type            | Null | Key | Default | Extra          |
+-------------------+-----------------+------+-----+---------+----------------+
| id                | bigint unsigned | NO   | PRI | NULL    | auto_increment |
| name              | varchar(255)    | NO   |     | NULL    |                |
| birth_year        | int             | NO   |     | NULL    |                |
| birth_month       | int             | NO   |     | NULL    |                |
| birth_day         | int             | NO   |     | NULL    |                |
| email             | varchar(255)    | NO   | UNI | NULL    |                |
| email_verified_at | timestamp       | YES  |     | NULL    |                |
| password          | varchar(255)    | NO   |     | NULL    |                |
| remember_token    | varchar(100)    | YES  |     | NULL    |                |
| created_at        | timestamp       | YES  |     | NULL    |                |
| updated_at        | timestamp       | YES  |     | NULL    |                |
+-------------------+-----------------+------+-----+---------+----------------
11 rows in set (0.01 sec)

上手く追加されていました!

おまけ: afterメソッド使用しないでカラム追加(従来のやり方)

public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->integer('birth_year')->after('name');
            $table->integer('birth_month')->after('birth_year');
            $table->integer('birth_day')->after('birth_month');
        });
    }

参考

Add Multiple Columns After a Column in Migrations

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?