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