44
29

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 5 years have passed since last update.

Laravelでテーブルのカラムを削除する方法

Posted at

Laravelでテーブルのカラムを削除する

  1. migrationファイルを作る
  2. 中身を記述する
  3. 実行!

migrationファイルを作る

`hoge' には該当のテーブル名を入れてください

migrationファイル作成
php artisan make:migration drop_column_hoges_column --table=hoges

テーブルカラムの削除処理を記述

upの方に削除処理を記述し、downの方にはカラム追加処理を記述しましょう。

以下の書き方で piyo カラムを削除することができます

<?php

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

class DropColumnHogesTable extends Migration
{
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up()
  {
    Schema::table('hoges', function (Blueprint $table) {
      $table->dropColumn('piyo');
    });
  }

  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
    Schema::table('hoges', function (Blueprint $table) {
      $table->boolean('piyo')->default(false);
    });
  }
}

テーブルのカラム削除をLaravelで実行する

実行時はこちら

php artisan migrate

そしてrollbackも実行しておきましょう。

44
29
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
44
29

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?