How to change a MySQL column data type using migration in Laravel?

I’m working on a Laravel project, and I need to change the column data type in my MySQL database from VARCHAR to DECIMAL. However, there is already data in the column, so I can’t just run php artisan migrate:refresh and lose all the data. Is there a way to update the column’s data type without losing any data? What is the best way to achieve this in Laravel?

You can follow the steps below to change the data type of a MySQL column from VARCHAR to DECIMAL in Laravel without losing your data.

First, create a new migration using the make:migration Artisan command:

php artisan make:migration alter_table_[yourtablenamehere]_change_[somecolumnname] --table=[yourtablenamehere]

A new migration file will be created, there In the `up` method of the migration file, use the following code to change the column data type.
public function up()
{
    Schema::table('table_name', function($table)
    {
        $table->decimal('column_name', 8, 2)->change();
    });

}

After creating the migration, run it using the migrate Artisan command:

php artisan migrate

Note: You need to install doctrine/dbal to make this work

composer require doctrine/dbal

This will apply the changes to the database, modifying the data type of the specified column.

I prefer this format

php artisan make:migration alter_table_[tablename]_table --table=[tablename]