How to Change Column Name and Data Type in Laravel Migration? (ok)

https://www.itsolutionstuff.com/post/how-to-change-column-name-and-data-type-in-laravel-migrationexample.html

C:\xampp\htdocs\reset\database\migrations\2022_05_20_180539_create_posts_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration {
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up() {
    Schema::create('posts', function (Blueprint $table) {
      $table->id();
      $table->string('title');
      $table->text('body');
      $table->boolean('is_publish')->default(0);
      $table->timestamps();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down() {
    Schema::dropIfExists('posts');
  }
}

Trước

C:\xampp\htdocs\reset\database\migrations\2022_05_20_180539_create_posts_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration {
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up() {
    Schema::create('posts', function (Blueprint $table) {
      $table->id();
      $table->string('title');
      $table->text('body');
      $table->boolean('is_publish')->default(0);
      $table->timestamps();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down() {
    Schema::dropIfExists('posts');
  }
}

Sau

C:\xampp\htdocs\reset\database\migrations\2022_05_20_180718_change_posts_table_column.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangePostsTableColumn extends Migration {
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up() {
    Schema::table('posts', function (Blueprint $table) {
      $table->renameColumn('title', 'name');
      $table->longText('body')->change();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down() {
    //
  }
}

How to Change Column Name and Data Type in Laravel Migration?

Hi Artisan,

This post is focused on laravel migration change column name. this example will help you how to change column type in laravel migration. step by step explain how to rename column name in laravel migration. i explained simply about how to change data type in laravel migration. follow bellow step for how to change column name in laravel migration.

I will give you two example of changing data type and rename column name using migration in laravel 6, laravel 7, laravel 8 and laravel 9 application.

First of all we need to install "doctrine/dbal" composer package.

Install Composer Package:

composer require doctrine/dbal

After successfully install composer package we can change data type and change column name using migration.

Let's see bellow example:

Migration for main table:

<?php  use Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;  class CreatePostsTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('posts', function (Blueprint $table) {            $table->bigIncrements('id');            $table->string('title');            $table->text('body');            $table->boolean('is_publish')->default(0);            $table->timestamps();        });    }      /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::dropIfExists('posts');    }}

Change Data Type using Migration:

body column text data type to long text data type here.

<?php  use Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;  class ChangePostsTableColumn extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::table('posts', function (Blueprint $table) {            $table->longText('body')->change();        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {              }}

Rename using Migration:

rename title column to name changed.

Read Also: How to Create Table using Migration in Laravel?

<?php  use Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;  class ChangePostsTableColumn extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::table('posts', function (Blueprint $table) {            $table->renameColumn('title', 'name');        });    }      /**     * Reverse the migrations.     *     * @return void     */    public function down()    {            }}

I hope it can help you...

Last updated