首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

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

发布于 2025-05-04 21:00:05
0
22

函数名称:Schema::dropCollection() 适用版本:Laravel 5.0+ 函数描述:该函数用于删除数据库中的集合(表)。它在Laravel框架的数据库迁移中使用。 用法: use...

函数名称: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集合,以便在回滚迁移时可以恢复。

评论
一个月内的热帖推荐
啊龙
Lv.1普通用户

9545

帖子

31

小组

3242

积分

赞助商广告
站长交流