前のページ

Laravelでテーブル作成後にカラムのデータ型を変更する方法

みなさんこんにちは、あべ いさぢです。


今回はテーブルを作成したあとにカラムのデータ型を変更する方法について解説します。


Laravelのバージョン

8系で動作の確認をしています。


doctrine/dbalパッケージのインストール

下記のコマンドを叩きます。

composer require doctrine/dbal



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

下記のコマンドを叩きます。


php artisan make:migration change_テーブル名_table --table テーブル名




マイグレーションファイルに記述

下記の記述をします。



public function up(){
    Schema::table('xxxxxx', function (Blueprint $table) {
        $table->データ型('カラム名')->change();
    });}

/**
 * Reverse the migrations.
 *
 * @return void
 */public function down(){
    Schema::table('xxxxxx', function (Blueprint $table) {
        $table->データ型('カラム名')->change();
    });}



4行目はデータ型を変更する為の記述です。


16行目は元々のデータ型を記述します。


マイグレーションファイルをテーブルに適用

下記のコマンドを叩きます。


php artisan migrate


これで完成です。















戻る