Ad

Is It Possible To Change The Datatype Of A Column From Int To Double Without Losing Data With Laravel Migration

I'm trying to change the datatype one of my column. My migration to create table was:

public function up()
    {
        Schema::create('place_ratings', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('places_id');
            $table->foreign('places_id')->references('id')->on('places');
            $table->integer('rating');
            $table->mediumText('comments');
            $table->timestamps();
        });
    }

I want to change the datatype of rating table from integer to float. I already have some data in my table and I don't want to lose them. So, how can I do that?

Ad

Answer

You should be able to change that column using a simple migration, see https://laravel.com/docs/5.8/migrations#modifying-columns for additional info:

You need to install composer require doctrine/dbal first for the ability to generate sql that changes these columns.

Schema::table('place_ratings', function (Blueprint $table) {
    $table->decimal('rating', 16, 4)->change();
});

The 16 and 4 means the total length and the decimal precision, so in this case it would have numbers of range 0.0000 (nothing before the dot, 4 after) to 000000000000.0000 (12 before the dot, 4 after, so 16 total).

Be sure to run it locally first to make sure you don't have unwanted side effects.

Ad
source: stackoverflow.com
Ad