English | 简体中文 | 繁體中文
查询

Schema::dropCollection()函数—用法及示例

「 删除数据库中的集合(表) 」


函数名称:Schema::dropCollection()

适用版本:Laravel 5.0+

函数描述:该函数用于删除数据库中的集合(表)。它在Laravel框架的数据库迁移中使用。

用法:

use Illuminate\Support\Facades\Schema;

Schema::dropCollection(string $collection)

参数:

  • $collection(必填):要删除的集合名称。

示例:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;

class DropUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 删除users集合
        Schema::dropCollection('users');
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // 创建users集合
        Schema::create('users', function ($collection) {
            $collection->increments('id');
            $collection->string('name');
            $collection->timestamps();
        });
    }
}

在上面的示例中,我们创建了一个名为DropUsersTable的迁移类。在up方法中,我们使用Schema::dropCollection函数删除了名为users的集合。在down方法中,我们使用Schema::create函数重新创建了users集合,以便在回滚迁移时可以恢复。

补充纠错
上一个函数: RRDGraph::saveVerbose()函数
下一个函数: scandir()函数
热门PHP函数
分享链接