Laravel迁移:删除特定的表

Noo*_*der 4 database-migration laravel

是否有任何方法/ laravel-command从生产服务器中删除特定的表?

haa*_*kym 5

设置迁移.

运行此命令以设置迁移:

php artisan make:migration drop_my_table
Run Code Online (Sandbox Code Playgroud)

然后,您可以像这样构建迁移:

<?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();
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

您当然可以放弃而不检查是否存在:

Schema::drop('my_table');
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

    Schema::dropIfExists('my_table');
}
Run Code Online (Sandbox Code Playgroud)

更多关于删除索引等文档:

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