Press ESC to close

Manoj Bist

How to Modify Type Of Column In Laravel Migration

 Hello Guys,

This is a quick example of how we can modify a column in Laravel
migrations.

You May like 

How to add a column in Laravel
Migration

How to drop a column in
Laravel Migration

In this article,  We will be discussing how we can modify a column’s data
type in the existing table using Laravel Migrations.

No worries, if you don’t know how to create migrations in Laravel, will discuss

that as well.

Here is a command to create a migration file in Laravel.

1. Go to your Laravel installation directory, open CMD ( For windows user ),
and copy the following code to create a migration.


php artisan make:migrations alert_your-table-name_table

The above command will create a migration file that will be used to modify a
column’s data type to the existing table. 

Now you have successfully created a migration file, now open that
file 

In the Up method, i wanted to make a column nullable so i added the following code

$table->string('my_column_name, 100)->nullable()->change();

Here nullable() is the function that will make the mentioned column
nullable.

And change() is the method that will actually modify the column’s
dataType..

The above method will only change the existing columns dataType in the
table.

You can also check if the column exists in the table with the hasColumn()
method. 


That’s it. In the Down method, you can add a column again.

Manoj Bist

A Passionate Web Developer/Designer With over 6 years of experience in the industry, Manoj Bist is a seasoned professional who combines technical expertise with a down-to-earth demeanor. As a lover of both technology and design, he thrives on creating seamless digital experiences that captivate and inspire.

Leave a Reply

Your email address will not be published. Required fields are marked *