且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel迁移:删除特定表

更新时间:2022-01-05 06:19:29

设置迁移

运行以下命令来设置迁移:

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

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

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