Schema::dropCollection()

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

首字母:S

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

同首字母函数