Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the best way to change column type in laravel?

I have column balance with data type double, but I want to change it for storing price in proper way. I want to make it decimal.

For that I am trying I am doing following

public function up()
{
    Schema::table('billing', function (Blueprint $table) {
        //
        $table->decimal('balance', 8, 2)->change();
    });
}

But I am confused about down function, should I revert it back, I mean the previous data type?

Can someone kindly guide me about that, I will be thank full. Thank you

like image 658
Imran Abbas Avatar asked Apr 16 '19 07:04

Imran Abbas


People also ask

How do I change a column to unique in Laravel migration?

$table->unique('slug'); So you add unique index to existing 'slug'.

How do I change the default value of a column in Laravel?

The various approaches to solving the Set Default Value For Column In Laravel Model problem are summarised in the following code. You can use change() method: Schema::table('users', function ($table) { $table->integer('active')->default(0)->change(); });

How do I change a table column in Laravel migration?

First of all we need to install "doctrine/dbal" composer package. After successfully install composer package we can change data type and change column name using migration. * Run the migrations. * Reverse the migrations.


2 Answers

You don't need to define a drop function for an up function which alters a column.

Because drop() is intended to delete a table and maybe cascade the action. And your initial billing migration file should already have a drop definition for deleting that table.

So you can safely keep the down() function empty in your new migration file

like image 77
Sapnesh Naik Avatar answered Sep 19 '22 10:09

Sapnesh Naik


The purpose of down method is to revert the change to state which was exactly same before running the up method of migration.If you are changing any user type in up() then it should be reverse in down() ideally.So your migration will end like this:

   public function up()
    {
      //double to decimal
    }

  public function down()
    {
     //decimal to double
    }
like image 24
svikramjeet Avatar answered Sep 19 '22 10:09

svikramjeet