函數(shù)名稱:Schema::getCollection()
適用版本:PHP 7.2.0 或更高版本
用法:Schema::getCollection() 方法用于獲取數(shù)據(jù)庫的集合(collection)對象。它返回一個實現(xiàn)了 Illuminate\Database\Schema\Builder
接口的對象,可以用于創(chuàng)建、修改和刪除數(shù)據(jù)庫表。
示例:
use Illuminate\Support\Facades\Schema;
// 獲取默認數(shù)據(jù)庫連接的集合對象
$collection = Schema::getCollection();
// 創(chuàng)建一個名為 "users" 的數(shù)據(jù)庫表
$collection->create('users', function($table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
// 修改已存在的數(shù)據(jù)庫表
$collection->table('users', function($table) {
$table->string('email')->nullable()->change();
});
// 刪除數(shù)據(jù)庫表
$collection->dropIfExists('users');
在上述示例中,首先通過 Schema::getCollection()
獲取默認數(shù)據(jù)庫連接的集合對象。然后,我們使用 create()
方法創(chuàng)建了一個名為 "users" 的數(shù)據(jù)庫表,該表包含一個自增的 id 字段、一個名為 name 的字符串字段和兩個默認的時間戳字段。接著,我們使用 table()
方法修改了已存在的數(shù)據(jù)庫表 "users",將名為 email 的字段修改為可為空。最后,使用 dropIfExists()
方法刪除了數(shù)據(jù)庫表 "users"。
請注意,示例中的代碼使用了 Laravel 框架的數(shù)據(jù)庫模塊。在使用 Schema::getCollection()
之前,確保已正確配置數(shù)據(jù)庫連接。