Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify a migration in Laravel?

I'm trying to modify a existing migration. Here is my current migration class:

class CreateLogForUserTable extends Migration {     public function up()     {         Schema::create('log_for_user', function (Blueprint $table) {             $table->increments('id');             $table->integer('user_id');             $table->string('table_name');             $table->string('error_message');             $table->unsignedTinyInteger('error_code');             $table->timestamps();         });     }      public function down()     {         Schema::drop('log_for_user');     } } 

I've executed the php artisan migrate command once. Now I need to add ->nullable() method to the error_message column. So I edited my migration, something like this:

. .     $table->string('error_message')->nullable(); . . 

But when I execute php artisan migrate again, it says:

Nothing to migrate.

How can I apply the new version of the migration?

like image 376
stack Avatar asked Dec 18 '16 05:12

stack


People also ask

Can I rename migration in Laravel?

Yes, you can name them however you want, but they will run in alphabetical order (which is why laravel timestamps them). Also you can change a migrations name after the fact (you've already run the migration).

How do I change the default value in Laravel migration?

In our case we are going to do second option - create new migration. If we roll back this migration, we need to remove default value from that field using migration and this is how to do it: Schema::table('photos', function (Blueprint $table) { $table->integer('order')->default(NULL)->change(); });


1 Answers

You should create a new migration using command:

php artisan make:migration update_error_message_in_log_for_user_table 

Then, in that created migration class, add this line, using the change method like this:

class UpdateLogForUserTable extends Migration {     public function up()     {         Schema::table('log_for_user', function (Blueprint $table) {             $table->string('error_message')->nullable()->change();         });     }      public function down()     {         Schema::table('log_for_user', function (Blueprint $table) {             $table->string('error_message')->change();         });     } } 

To make these changes and run the migration, use the command:

php artisan migrate 

and to rollback the changes, use the command:

php artisan migrate:rollback 

You may rollback a limited number of migrations by providing the step option to the rollback command. For example, the following command will rollback the last five migrations:

php artisan migrate:rollback --step=5 

See more about Modifying columns with Migration

like image 152
Saumya Rastogi Avatar answered Sep 30 '22 13:09

Saumya Rastogi