Laravel 迁移:删除特定表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37249235/
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 13:48:37  来源:igfitidea点击:

Laravel migrations: dropping a specific table

laraveldatabase-migration

提问by Noob Coder

Is there any way/laravel-command to drop a specific table from the production server?

有没有办法/laravel-command 从生产服务器中删除特定的表?

回答by haakym

Set up a migration.

设置迁移。

Run this command to set up a migration:

运行此命令以设置迁移:

php artisan make:migration drop_my_table

Then you can structure your migration like this:

然后你可以像这样构建你的迁移:

<?php

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

class DropMyTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // drop the table
        Schema::dropIfExists('my_table');
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // create the table
        Schema::create('my_table', function (Blueprint $table) {
            $table->increments('id');
            // .. other columns
            $table->timestamps();
        });
    }
}

You can of course just drop and not check for existence:

您当然可以直接删除而不检查是否存在:

Schema::drop('my_table');

Read further in the docs here:

在此处的文档中进一步阅读:

https://laravel.com/docs/5.2/migrations#writing-migrations

https://laravel.com/docs/5.2/migrations#writing-migrations

You may also have to consider dropping any existing foreign keys/indexes, for example if you wanted to drop a primary key:

您可能还需要考虑删除任何现有的外键/索引,例如,如果您想删除主键:

public function up()
{
    Schema::table('my_table', function ($table) {
        $table->dropPrimary('my_table_id_primary');
    });

    Schema::dropIfExists('my_table');
}

More in the docs in dropping indexes etc here:

更多关于删除索引等的文档在这里:

https://laravel.com/docs/5.2/migrations#dropping-indexes

https://laravel.com/docs/5.2/migrations#dropping-indexes