0
0

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 1 year has passed since last update.

【Laravel migration】テーブルの編集1(カラムの追加、カラムの削除)

Posted at

Laravelのマイグレーション

データベースの作成や編集、削除などを管理する方法として、PHPフレームワークのLaravelには「マイグレーション」というものが備わっています。
本記事では、マイグレーションで生成されたテーブルを編集する方法(カラムの追加、カラムの削除)について書きます。

テーブルの作成については、下の記事をご参照ください。

開発環境

XAMPP
MySQL 5.2
Laravel 10.1
php 8.1

1. カラムの追加

カラムを追加するマイグレーションファイルを作成します。

$ php artisan make:migration add_title_to_posts_table

上の例では、既存のpostsテーブルに対して、titleカラムを追加しようとしています。

カラム追加のマイグレーションファイル名は通例、下の文法で描きます。
これで、一目で何がどこに追加されるのかがわかります。

add_(追加するカラム名)_to_(追加されるテーブル名)_table

生成されたマイグレーションファイルがこちら。

2023_06_07_145030_add_title_to_posts_table.php
<?php

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

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

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

新しく生成されたマイグレーションファイルの「up」と「down」それぞれに追加したいカラム情報を挿入します。

2023_06_07_145030_add_title_to_posts_table.php
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->string('title'); //titleカラムの追加
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropColumn('title'); //ロールバック時にtitleカラムを削除する
        });
    }
};

追加ばかりに気を取られて「down」を設定していないと、ロールバック時に不具合を起こす原因になります。忘れず記載しましょう。

最後にマイグレーションを実行すれば、カラムの追加完了です。

php artisan migrate

2. カラムの削除

カラムを削除するマイグレーションファイルを作成します。

$ php artisan make:migration drop_title_from_posts_table

上の例では、先ほどのpostsテーブルから、titleカラムを削除しようとしています。

カラム削除のマイグレーションファイル名は通例、下の文法で描きます。

drop_(削除するカラム名)_from_(削除されるテーブル名)_table

生成されたマイグレーションファイルがこちら。

2023_06_07_152129_drop_title_from_posts_table.php
<?php

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

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

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

新しく生成されたマイグレーションファイルの「up」と「down」それぞれに削除するカラム情報を挿入します。(追加の場合と「up」「down」の中身が逆になります。)

2023_06_07_152129_drop_title_from_posts_table.php
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropColumn('title'); //titleカラムの削除
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->string('title'); //ロールバック時にtitleカラムを追加し直す
        });
    }
};

最後にマイグレーションを実行すれば、カラムの削除完了です。

php artisan migrate
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?