LoginSignup
7
3

More than 3 years have passed since last update.

【Laravel】カラム型の変更

Posted at

はじめに

知識を整理するための個人的な備忘録です。

一度作成したカラム型の変更方法をまとめます。

方法

まずLaravel6.xでは、以降のカラムタイプのみ変更可能。

bigInteger、binary、boolean、date、dateTime、dateTimeTz、decimal、integer、json、longText、mediumText、smallInteger、string、text、time、unsignedBigInteger、unsignedInteger and unsignedSmallInteger

1.ライブラリのインストール

下記のコマンドでdoctrine/dbalをインストール

$ composer require doctrine/dbal

2.マイグレーションファイルの作成

ファイル名はなんでも良いが、何の内容のmigrationファイルが実行されたかがわかる名前をつけるのが良い。

$ php artisan make:migration change_対象のカラム名_変更前カラム型_to_変更後カラム型_on_テーブル名_table --table=テーブル名

3.マイグレーションファイルに追記

database/migrations/YYYY_MM_DD_XXXXXX_change_カラム名_変更前カラム型_to_変更後カラム型_on_テーブル名_table.php
<?php

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

class Changeカラム名変更前カラム型To変更後カラム型Onテーブル名Table extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('テーブル名', function (Blueprint $table) {
            //下記を追記
            $table->変更後カラム型('カラム名')->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('テーブル名', function (Blueprint $table) {
            //下記を追記
            $table->変更前カラム型('カラム名')->change();
        });
    }
}

4.マイグレート

$ php artisan migrate

ここでエラーが出なければ良いが、以下のエラーが出た時の解決方法

Symfony\Component\Debug\Exception\FatalThrowableError  : Class 'Doctrine\DBAL\Driver\PDOMySql\Driver' not found

参考

*Laravel 6.x データベース:マイグレーション

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