LoginSignup
0
1

More than 1 year has passed since last update.

【Laravel】 DBのカラムを追加・編集・削除をする方法

Posted at

今回は、LaravelでDBのカラムを追加や編集、削除を行う方法を簡単にまとめてみます。

使用環境

Laravel 6.2
MySQL 8.0

前提

hogeテーブルを作っているとします。

20xx_xx_xx_130000_create_hoge_table.php
class CreateHogeTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('hoge', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('hoge');

            $table->timestamps();
        });
    }

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

カラムを追加

すでにあるテーブルにカラムを追加します。
以下を実行します。

ターミナル
php artisan make:migration add_hoge_id_to_hoge_table --table=hoge

ここのadd_hoge_id_to_hoge_tableは任意の名前です。
後に確認したときに何をどのテーブルに追加するかと明記した方がわかりやすいかなと思います。
ここで重要なのが--tableオプションです。このオプションの後はテーブル名を記載しましょう。

実行するとmigrationファイルが生成されます。
追加したいカラムを記載します。

php
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('hoge', function (Blueprint $table) {
            $table->string('hoge_id'); //追加したいカラム
        });
    }

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

後は、migrateをすれば追加完了です!

ターミナル
php artisan migrate

カラムを編集する

次は、カラムを編集します。
先ほど追加したカラムをnullを許容するように設定します。

migrationファイルを作成する前に、ライブラリをインストールしないとできません。
以下を実行してインストールしましょう。

ターミナル
composer require doctrine/dbal

次にmigrationファイルを作成します。

ターミナル
php artisan make:migration change_hoge_id_to_nullable_on_hoge_table --table=hoge

生成されたファイルに記載していきます。

php
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('hoge', function (Blueprint $table) {
            $table->string('hoge_id')->nullable()->change(); //編集したいカラム
        });
    }

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

後は、migrateをすれば編集完了です!

ターミナル
php artisan migrate

カラムの削除

先ほど追加、編集したカラムを削除します。

ターミナル
php artisan make:migration drop_column_hoge_id_column --table=hoge

生成されたファイルに記載していきます。

php
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('hoge', function (Blueprint $table) {
            $table->dropColumn('hoge_id'); //削除したいカラム
        });
    }

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

後は、migrateをすれば削除完了です!

ターミナル
php artisan migrate

以上が、カラムの追加、編集、削除の方法でした。

参考文献

https://qiita.com/usaginooheso/items/6f307a15b5f7d5dd981f
https://qiita.com/kambe0331/items/1fe65fc001b958efb6d0

ありがとうございました!

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