代码之家  ›  专栏  ›  技术社区  ›  Deeven

Laravel的上下迁移方法是如何工作的?

  •  7
  • Deeven  · 技术社区  · 7 年前

    在这段代码中,表在up方法中创建,在down()方法中删除。运行迁移时,会创建表,但不会删除。我可以通过什么方式触发down方法,以便更好地了解这两种方法的工作原理?

    <?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');
        }
    }
    
    3 回复  |  直到 6 年前
        1
  •  13
  •   Community CDub    5 年前

    在您的示例中:

    php artisan migrate 将运行 up() 方法

    php artisan migrate:rollback 将运行 向下() 方法

    阅读优秀的文档: https://laravel.com/docs/5.7/migrations#migration-structure

        2
  •  6
  •   yosober    5 年前

    您可以添加 dropIfExists 而不是 drop

    如果您只想删除特定的迁移文件,您应该在命令中编写代码,如下所示:

    php artisan migrate:rollback --path=\database\migrations\flights.php
    
        3
  •  0
  •   Felipe Perucci M. Do Amaral    7 年前

    尝试:

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