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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49056542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:27:35  来源:igfitidea点击:

How does Laravel's migration up and down method work?

phplaraveldatabase-migration

提问by Deeven

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?

在这段代码中,表是在 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');
    }
}

回答by mikemols

In your example:

在你的例子中:

php artisan migratewill run your up()method.

php artisan migrate将运行您的up()方法。

php artisan migrate:rollbackwill run your down()method.

php artisan migrate:rollback将运行您的down()方法。

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

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

回答by Felipe Perucci M. Do Amaral

Try:

尝试:

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

回答by yosober

You sohuld add dropIfExistsinstead of drop

你应该添加dropIfExists而不是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