In this tutorial, you will go to learn how to change the column type by using laravel migration.
If you are doing progressive development, chances are that due to a change in the requirement , you have to change the column type.
We will be considering the example of the users
here, where in the users
table we had the name
column with the string
type but want to change to the longText
.
By the end of the article, you will be able to change column data type laravel migration.
Lets get started!
Table of Contents
Step 1: Install Laravel
Let’s start with installing the laravel itself in the folder name change_column_type
.
composer create-project --prefer-dist laravel/laravel change_column_type
Step 2: Update Env file with DB Credentials
Now go the .env
file and change the DB_PORT
, DB_DATABASE
, DB_USERNAME
and DB_PASSWORD
with your DB Credentials.
Step 3: Migrate Database
Lets now migrate, so that we can have the current tables in the database:
php artisan migrate
Step 4: Create New Migration for Changing Column Type
Since we want to rename the users
table column name
. We want to change the name
type from string
to longText
.
So in order to do that, let’s create new migration file called change_users_table_name_column_type
.
php artisan make:migration change_users_table_name_column_type
It will create the new
migration file.change_users_table_name_column_type
Step 5: Edit the Migration file
Now you need to go to the database/migrations
and open change_users_table_name_column_type
migration file, you will see something like this:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangeUsersTableNameColumnType extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
Step 6: Change the Column Type
In the up()
function and add the following code like this:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->longText('name')->change();
});
}
You can see that first we typed the column type which we want longText
, then in the arguments, we provided the column name itself which was the name
followed by the change()
function.
Step 7: Run the migrate command
Now migrate once again:
php artisan migrate
Step 8: Install DABL Package if you getting Exception
If by any chance, you are getting the following exception, you need to install package doctrine/dbal

Install the doctrine/dbal
package by typing the following command in terminal:
composer require doctrine/dbal
It is mentioned in the laravel documentation as well that to modify the column you need that package to save its state.
Before modifying a column, be sure to add the doctrine/dbal dependency to your composer.json file. The Doctrine DBAL library is used to determine the current state of the column and create the SQL queries needed to make the specified adjustments to the column:
Step 9: Do the migrate again
Run the migration once again:
php artisan migrate
You will see that the users
table column name name
type has been changed from string to the longText
.
Conclusion:
Hope in the above post, you have learned today on how to change the column type using laravel migration.
Check this article, if you want to change the column name using laravel migration.
Read More: How to Change Table Name using Laravel Migration?
I will see you the next one. Keep Coding!
Write a Reply or Comment