What is use of the laravel migration down() method

What is use of laravel migration down method. Why we use laravel migration down() method. difference between laravel migration up and down method. Laravel migration rollback command.


Laravel migration down() method

Laravel migration down() method is just opposite of up() method it use for rollback up method process. We take two examples for understanding its work, In first example when we create a new table migration then in down method write code for drop that table.

public function up()
{
  Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->float('price', 8,2);
    $table->string('image');
    $table->text('description')->nullable();
    $table->enum('status', ['0','1'])->default(0);
    $table->timestamps();
  });
}

public function down()
{
  Schema::dropIfExists('products');
}

up() method run by "php artisn migrate" and create new products table and after create we want to delete products table then run down() method by "php artisan migrate:rollback" command.

In other example when we add new column migration then in down method write code for delete that columns

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

public function down()
{
  Schema::table('products', function($table) {
    $table->dropColumn(['color', 'brand']);
  });
}
php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Random tutorial links