LoginSignup
4
2

More than 5 years have passed since last update.

[ メモ ] laravel5.6 マイグレーション データベース(テーブル)のカラムの変更・追加・カラム名変更

Last updated at Posted at 2018-06-14

テーブルのカラムの型を変更

今回、文字型から整数型(INT)に変更した時に、上手くいかなかったのでメモ

  • 変更用のマイグレーションファイルを作成
$ php artisan make:migration add_users_table --table=users

:exclamation: add_(マイグレーションファイル名)_table
:exclamation: --table=(テーブル名)

2018_06_01_11111_add_users_table.php
<?php

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

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

        });
    }

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

        });
    }
}

このようなマイグレーションファイルができたら

  • upメソッドの中にカラム属性の変更をかく
    • changeメソッドは、存在するカラムを新しいタイプへ変更するか、カラムの属性を変更
2018_06_01_11111_add_users_table.php
   /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->integer('status')->change();
        });
    }
  • downメソッドに削除したいカラムをかく
2018_06_01_11111_add_users_table.php
   /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('status');
        });
    }
  • マイグレーションを実行して、カラムを変更する
$ php artisan migrate

テーブルにカラムを追加

  • マイグレーションファイルを作成

  • upメソッドの中に追加するカラムをかく

    • after( ) 追加するカラムを入れたい場所を指定
    • nullable( ) NULL対応したい場合に使用
2018_06_01_11111_add_users_table.php
   /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('email')->after('name')->nullable();
        });
    }
2018_06_01_11111_add_users_table.php
   /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('email');
        });
    }
  • マイグレーションを実行して、カラムを追加する
$ php artisan migrate

テーブルのカラム名を変更

  • マイグレーションファイルを作成
  • laravelのデフォルトでは、カラム名の変更はできない
  • doctrine/dbalをインストールしないといけない
$ composer require doctrine/dbal
  • renameColumn( ) を使用する
    • upメソッド内には、'現在のカラム名', '変更後のカラム名'の順番
2018_06_01_11111_add_users_table.php
   /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->renameColumn('email','tel');
        });
    }
  • downメソッド内には、'変更後のカラム名', '現在のカラム名'の順番
2018_06_01_11111_add_users_table.php
   /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->renameColumn('tel','email');
        });
    }
  • マイグレーションを実行して、カラム名を変更する
$ php artisan migrate
4
2
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
4
2