Laravelでデータベースのカラム設定を変更する方法を書きます。
○以下の記事を参考にさせていただきました。ありがとうございました。
Laravel MySQL DBのカラムタイプを途中で変更する
スポンサードサーチ
Doctrine DBALライブラリをインストールする
1 | composer require doctrine/dbal |
マイグレーションファイルを作成する
1 | php artisan make:migration change_カラムタイプ変更するカラム名_既存カラムタイプ_to_変更後のカラムタイプ_on_所属テーブル名_table --table=所属テーブル名 |
マイグレーションファイルを修正する
先ほど作成したマイグレーションファイルを修正します。以下の場合はpostsテーブルのimage_pathのNullを許可するようにしています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class ChangeImagePathNullablefalseToNullableOnPostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('posts', function (Blueprint $table) { // $table->string('image_path')->nullable()->change(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('posts', function (Blueprint $table) { // $table->string('image_path')->change(); }); } } |
マイグレート
1 | php artisan migrate |
MySQLにログインしてカラム設定が変わっていれば成功です。
スポンサードサーチ
スポンサードサーチ