Laravel Migration Change Datatype String to Integer Example
Hi Dev,
Now, let's see post of laravel migration change string to integer. This tutorial will give you a simple example of how to change varchar to integer in laravel migration. you can understand a concept of laravel migration change datatype string to integer. you'll learn laravel migration update data type string to integer.
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 string to integer in laravel migration.
In this example, i will change viewer column datatype varchar(255) to integer.
So, let's see the simple example of laravel migration change string to integer.
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->integer('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` INT 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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Migration Change Datatype Text to Longtext Example
- Laravel Artisan Command to Create Migration and Model Example
- How to Drop Soft Delete from Table using Laravel Migration?
- Laravel Migration Add Column After Column Example
- Laravel Migration Default Value Current Timestamp Example
- Laravel Migration Add Enum Column Example
- How to Rollback Migration in Laravel?
- Laravel Migration Custom Foreign Key Name Example
- How to Add Index in Laravel Migration?
- How to Add Foreign Key in Laravel Migration?
- Laravel Migration - How to Add New Column in Existing Table ?
- How to Change Table Name using Laravel Migration?
- How to Remove Column from Table in Laravel Migration?
- How to Change Column Name and Data Type in Laravel Migration?