John Does Legacy
John Does Legacy

Reputation: 1519

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.

Upvotes: 5

Views: 28656

Answers (3)

Alexey Mezenin
Alexey Mezenin

Reputation: 163798

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.

Upvotes: 10

Squix Dev
Squix Dev

Reputation: 51

You might use php artisan migrate:refresh to recreate the table (after editing the original migration).

Beware, it deletes your tables ! Useful for a quick fix on a new install though

Upvotes: 3

Grant
Grant

Reputation: 6329

Just thought I'd sum up what I did to achieve this, to elaborate on the answer above.

php artisan make:migration change_yourcolumn_to_float_in_yourtable

Then, inside of that migration, in the up() method, I added something like the following:

Schema::table('yourtable', function (Blueprint $table) {
    $table->float('yourcolumn')->change();  // Changing from int to float, ie.
});

Then you may need to add a package, to make database modifications like this: Enter into the console: composer require doctrine/dbal

Then you can go ahead and php artisan migrate to commit the change.

Upvotes: 2

Related Questions