9

In this code the table is created in the up method and deleted in the down() method. When I run the migration, the table is created but not deleted. In what way can I trigger the down method, so that I get a better understanding on how the two methods work?

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFlightsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('flights');
    }
}
mikemols
  • 824
  • 10
  • 17
Deeven
  • 413
  • 1
  • 5
  • 14
  • 1
    https://laravel.com/docs/5.6/migrations#rolling-back-migrations – Alexey Mezenin Mar 01 '18 at 18:41
  • You have any error in your first trying migration ? – Felipe Perucci M. Do Amaral Mar 01 '18 at 18:45
  • I copied the code from the documentation. I ran the code the table is getting created. – Deeven Mar 01 '18 at 18:46
  • 3
    The `down` method exists to run when the migration is rolled back, e.g: `php artisan migrate:rollback` will delete the table `flights`. You are expected to write code to reverse what was done in `up`, e.g: if `up` adds a new column; `down` deletes it. The down method only runs when you roll back the migration. – sam Mar 01 '18 at 18:54
  • Also there is create method to create a new table but there is the table method to update existing tables. How to use those? I mean create method will go in one class and when I want to edit the existing table should I create a new class and use the table method? – Deeven Mar 01 '18 at 19:01
  • The `up` method can contain whatever you like; you can create tables, update tables, delete tables, do data transformation: the `up` method is just like any other class method, it has no constraints. You should read the [migrations](https://laravel.com/docs/5.6/migrations) documentation to understand the functionality available to you; it'll explain better than I can in this comment. – sam Mar 01 '18 at 20:15
  • @Deeven Try reading the documentation. It’s explained there: https://laravel.com/docs/master/migrations – Martin Bean Oct 16 '18 at 14:04

5 Answers5

16

In your example:

php artisan migrate will run your up() method.

php artisan migrate:rollback will run your down() method.

Read up on the excellent docs: https://laravel.com/docs/5.7/migrations#migration-structure

Community
  • 1
  • 1
mikemols
  • 824
  • 10
  • 17
6

You sohuld add dropIfExists instead of drop

and if you wanna just drop the specific migration file you should write your code in command like this:

php artisan migrate:rollback --path=\database\migrations\flights.php
yosober
  • 345
  • 3
  • 16
  • You probably want to run `--path=/\database/\migrations/\flights.php` instead of `--path=\database\migrations\flights.php` because the example above executes `databasemigrationsflights.php`. – CupOfJava Jan 08 '23 at 07:53
1

In Laravel, We have multiple migration options available that will be used to migrate the table or roll back the table using the up() and down() methods.

The up() method is mainly used to perform certain operations on tables like creating tables, adding new fields, updating tables, etc...

While the down() method is used to rollback the migration like delete tables, delete columns from table, etc...

You can create a new migration by using the following command

php artisan make migration create_customers_table

Here customers is a table name.

Now We have 2 methods available that will help you to migrate or rollback the data.

  1. up(): php artisan migrate
  2. down(): php artisan migrate:rollback
0

Try:

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

Basically, up() is for creating tables or altering tables in the database, and down() is for reverting those changes. Note: both of these methods create your own logic that how you want to go about making changes.

calling these methods:

  1. up() php artisan migrate
  2. down() php artisan migrate:rollback
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
Zaryabro1
  • 36
  • 4