Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Migrations change/update

How do I properly change / update my migration, I am using Laravel 5.1.

The Docs said that I should use change to update the column e.g.:

$table->string('color', 10)->change();

But where do I have to put it in the migration and what command do I have to use to update just a simply php artisan migrate?

I tried this so far:

public function up()
{
   Schema::create('products', function (Blueprint $table)) {
        $table->string('color', 5);
   });

   Schema::table('products', function (Blueprint $table)) {
        $table->string('color', 10)->change();
   });
}

And used this command:

php artisan migrate

But it did not change.

like image 857
John Does Legacy Avatar asked Dec 19 '22 17:12

John Does Legacy


1 Answers

First, create new migration to change existing table column(s).

php artisan make:migration update_products_table

Then you should do this inside up() method in your new migration:

Schema::table('products', function (Blueprint $table) {
    $table->string('color', 10)->change();
});

So, you'll have two migrations. First did already create products table with wrong length of the color string.

Second migration will change color string length from 5 to 10.

After you created seconds migration, run php artisan migrate again and changes will apply to a table.

like image 138
Alexey Mezenin Avatar answered Jan 01 '23 06:01

Alexey Mezenin