ItSolutionStuff.com

Laravel Migration Change Datatype Integer to String Example

By Hardik Savani β€’ November 5, 2023
Laravel

Hi Developer,

In this short tutorial, we will cover a laravel migration change integer to string. This post will give you a simple example of how to change integer to varchar in laravel migration. I explained simply step by step laravel migration change datatype integer to string. This tutorial will give you a simple example of laravel migration update data type integer to string. Alright, let’s dive into the details.

Laravel migration provides way to add column name and datatype. But if you need to change column datatype then you have to install doctrine/dbal package to change datatype. In this example i will show you two ways to change datatype integer to string in laravel migration.

In this example, i will change viewer column datatype integer to varchar(255).

So, let's see the simple example of laravel migration change integer to string.

Install doctrine/dbal: optional

First of all we need to install "doctrine/dbal" composer package. This package allow to use change() method to update datatype using laravel migration.

composer require doctrine/dbal

Default Created Table

Here, you will see the default created table screenshot.

Way 1: Create Migration

Create new migration using following command:

php artisan make:migration change_datatype_column

Now, You can update it as like the bellow:

database/migrations/migration_name.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration

{

/**

* Run the migrations.

*/

public function up(): void

{

Schema::table('posts', function (Blueprint $table) {

$table->string('viewer')->change();

});

}

/**

* Reverse the migrations.

*/

public function down(): void

{

Schema::table('posts', function (Blueprint $table) {

});

}

};

Now, you are ready to run migration command:

php artisan migrate

You will see the layout as like the below:

Way 2: Create Migration

Create new migration using following command:

php artisan make:migration change_datatype_column

Now, You can update it as like the bellow:

database/migrations/migration_name.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration

{

/**

* Run the migrations.

*/

public function up(): void

{

\DB::statement('ALTER TABLE `posts` CHANGE `viewer` `viewer` VARCHAR(255) NULL DEFAULT NULL;');

}

/**

* Reverse the migrations.

*/

public function down(): void

{

Schema::table('posts', function (Blueprint $table) {

});

}

};

Now, you are ready to run migration command:

php artisan migrate

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

Laravel Migration Change Datatype Varchar to Text Example

Read Now β†’
β˜…

Laravel Migration Change Datatype Text to Longtext Example

Read Now β†’
β˜…

Laravel Artisan Command to Create Migration and Model Example

Read Now β†’
β˜…

How to Drop Soft Delete from Table using Laravel Migration?

Read Now β†’
β˜…

Laravel Migration Add Column After Column Example

Read Now β†’
β˜…

How to Change Column Length using Laravel Migration?

Read Now β†’
β˜…

How to Update Enum Value in Laravel Migration?

Read Now β†’
β˜…

Laravel Migration Enum Default Value Example

Read Now β†’
β˜…

Laravel Migration Custom Index Name Example

Read Now β†’
β˜…

Laravel Migration Custom Foreign Key Name Example

Read Now β†’
β˜…

How to Add Foreign Key in Laravel Migration?

Read Now β†’
β˜…

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

Read Now β†’
β˜…

How to Create Table using Migration in Laravel?

Read Now β†’
β˜…

How to Drop Foreign Key Constraint in Laravel Migration?

Read Now β†’