# 數據表結構
### 介紹
遷移和種子文件使您可以構建,修改和填充數據庫表。它們主要由[插件更新文件](https://octobercms.com/docs/plugin/updates)使用,并與插件的版本歷史記錄配對。所有類都存儲在`updates`插件目錄中。遷移過程應該講述一個有關數據庫歷史記錄的故事,并且可以向前和向后播放該故事以建立和刪除表。
### [](https://octobercms.com/docs/database/structure#migration-structure)遷移結構
遷移文件應定義一個擴展`October\Rain\Database\Updates\Migration`該類的類,并包含兩個方法:`up`和`down`。該`up`方法用于向數據庫中添加新表,列或索引,而該`down`方法應簡單地反轉該`up`方法執行的操作。在這兩種方法中,您都可以使用[模式構建器](https://octobercms.com/docs/database/structure#creating-tables)來表達性地創建和修改表。例如,讓我們看一下創建`october_blog_posts`表的示例遷移:
~~~
<?php namespace Acme\Blog\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('october_blog_posts', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('title');
$table->string('slug')->index();
$table->text('excerpt')->nullable();
$table->text('content');
$table->timestamp('published_at')->nullable();
$table->boolean('is_published')->default(false);
$table->timestamps();
});
}
public function down()
{
Schema::drop('october_blog_posts');
}
}
~~~
### [](https://octobercms.com/docs/database/structure#creating-tables)建立表格
若要創建新的數據庫表,請使用外觀`create`上的方法`Schema`。該`create`方法接受兩個參數。第一個是表的名稱,第二個是的`Closure`,它接收用于定義新表的對象:
~~~
Schema::create('users', function ($table) {
$table->increments('id');
});
~~~
當然,在創建表時,您可以使用模式構建器的任何[列方法](https://octobercms.com/docs/database/structure#creating-columns)來定義表的列。
#### 檢查表/列是否存在
您可以使用`hasTable`和`hasColumn`方法輕松檢查表或列是否存在:
~~~
if (Schema::hasTable('users')) {
//
}
if (Schema::hasColumn('users', 'email')) {
//
}
~~~
#### 連接和存儲引擎
如果要在不是默認連接的數據庫連接上執行架構操作,請使用以下`connection`方法:
~~~
Schema::connection('foo')->create('users', function ($table) {
$table->increments('id');
});
~~~
要為表設置存儲引擎,請`engine`在模式構建器上設置屬性:
~~~
Schema::create('users', function ($table) {
$table->engine = 'InnoDB';
$table->increments('id');
});
~~~
### [](https://octobercms.com/docs/database/structure#renaming-and-dropping-tables)重命名/刪除表
要重命名現有數據庫表,請使用以下`rename`方法:
~~~
Schema::rename($from, $to);
~~~
要刪除現有表,可以使用`drop`或`dropIfExists`方法:
~~~
Schema::drop('users');
Schema::dropIfExists('users');
~~~
### [](https://octobercms.com/docs/database/structure#creating-columns)創建列
要更新現有表,我們將`table`在`Schema`外觀上使用方法。與`create`方法類似,該`table`方法接受兩個參數,表的名稱和`Closure`,它接收一個可用于向表中添加列的對象:
~~~
Schema::table('users', function ($table) {
$table->string('email');
});
~~~
#### 可用的列類型
當然,架構構建器包含各種在創建表時可以使用的列類型:
| 命令 | 描述 |
| --- | --- |
| `$table->bigIncrements('id');` | 使用等效的“ UNSIGNED BIG INTEGER”遞增ID(主鍵)。 |
| `$table->bigInteger('votes');` | 相當于數據庫的BIGINT。 |
| `$table->binary('data');` | 相當于數據庫的BLOB。 |
| `$table->boolean('confirmed');` | 相當于數據庫的BOOLEAN。 |
| `$table->char('name', 4);` | 等同于CHAR的長度。 |
| `$table->date('created_at');` | 對應于數據庫的DATE。 |
| `$table->dateTime('created_at');` | 相當于數據庫的DATETIME。 |
| `$table->decimal('amount', 5, 2);` | 相當于DECIMAL的精度和規模。 |
| `$table->double('column', 15, 8);` | 精確度為DOUBLE,精度為15位,小數點后為8位。 |
| `$table->enum('choices', ['foo', 'bar']);` | 相當于數據庫的ENUM。 |
| `$table->float('amount');` | 相當于數據庫的FLOAT。 |
| `$table->increments('id');` | 使用等效的“ UNSIGNED INTEGER”遞增ID(主鍵)。 |
| `$table->integer('votes');` | 相當于數據庫的INTEGER。 |
| `$table->json('options');` | 與數據庫等效的JSON。 |
| `$table->jsonb('options');` | 相當于數據庫的JSONB。 |
| `$table->longText('description');` | LONGTEXT等效于數據庫。 |
| `$table->mediumInteger('numbers');` | 相當于數據庫的MEDIUMINT。 |
| `$table->mediumText('description');` | 相當于數據庫的MEDIUMTEXT。 |
| `$table->morphs('taggable');` | 添加INTEGER`taggable_id`和STRING`taggable_type`。 |
| `$table->nullableTimestamps();` | 與相同`timestamps()`,但允許NULL。 |
| `$table->rememberToken();` | 添加`remember_token`為VARCHAR(100)NULL。 |
| `$table->smallInteger('votes');` | 相當于數據庫的SMALLINT。 |
| `$table->softDeletes();` | “添加”`deleted_at`列用于軟刪除。 |
| `$table->string('email');` | VARCHAR等效列。 |
| `$table->string('name', 100);` | VARCHAR等效于長度。 |
| `$table->text('description');` | 等同于數據庫的TEXT。 |
| `$table->time('sunrise');` | 相當于數據庫的TIME。 |
| `$table->tinyInteger('numbers');` | 相當于數據庫的TINYINT。 |
| `$table->timestamp('added_on');` | 相當于數據庫的TIMESTAMP。 |
| `$table->timestamps();` | 添加`created_at`和`updated_at`列。 |
#### 列修飾符
除了上面列出的列類型之外,添加列時還可以使用其他幾個列“修飾符”。例如,要使列“可為空”,可以使用以下`nullable`方法:
~~~
Schema::table('users', function ($table) {
$table->string('email')->nullable();
});
~~~
以下是所有可用的列修飾符的列表。此列表不包括[索引修飾符](https://octobercms.com/docs/database/structure#creating-indexes):
| 修飾符 | 描述 |
| --- | --- |
| `->nullable()` | 允許將NULL值插入到列中 |
| `->default($value)` | 為列指定一個“默認”值 |
| `->unsigned()` | 將`integer`列設置為`UNSIGNED` |
| `->first()` | 將列“ first”放在表中(僅適用于MySQL) |
| `->after('column')` | 將列放在另一列“之后”(僅適用于MySQL) |
| `->comment('my comment')` | 在列中添加評論(僅適用于MySQL) |
### [](https://octobercms.com/docs/database/structure#modifying-columns)修改列
該`change`方法允許您將現有列修改為新類型,或修改列的屬性。例如,您可能希望增加字符串列的大小。要查看實際的`change`方法,讓我們將`name`列的大小從25增加到50:
~~~
Schema::table('users', function ($table) {
$table->string('name', 50)->change();
});
~~~
我們還可以將列修改為可為空:
~~~
Schema::table('users', function ($table) {
$table->string('name', 50)->nullable()->change();
});
~~~
#### [](https://octobercms.com/docs/database/structure#renaming-columns)重命名列
要重命名列,可以使用`renameColumn`“模式”構建器上的方法:
~~~
Schema::table('users', function ($table) {
$table->renameColumn('from', 'to');
});
~~~
> **注意:**`enum`目前不支持使用列重命名表中的列。
### [](https://octobercms.com/docs/database/structure#dropping-columns)刪除列
要刪除列,請使用`dropColumn`“模式”構建器上的方法:
~~~
Schema::table('users', function ($table) {
$table->dropColumn('votes');
});
~~~
您可以通過將列名稱數組傳遞給`dropColumn`方法來從表中刪除多個列:
~~~
Schema::table('users', function ($table) {
$table->dropColumn(['votes', 'avatar', 'location']);
});
~~~
### [](https://octobercms.com/docs/database/structure#creating-indexes)創建索引
模式構建器支持幾種類型的索引。首先,讓我們看一個示例,該示例指定一列的值應唯一。要創建索引,我們可以簡單地將`unique`方法鏈接到列定義上:
~~~
$table->string('email')->unique();
~~~
或者,您可以在定義列之后創建索引。例如:
~~~
$table->unique('email');
~~~
您甚至可以將列數組傳遞給索引方法以創建復合索引:
~~~
$table->index(['account_id', 'created_at']);
~~~
在大多數情況下,應手動為索引指定一個名稱作為第二個參數,以避免系統自動生成太長的名稱:
~~~
$table->index(['account_id', 'created_at'], 'account_created');
~~~
#### 可用的索引類型
| 命令 | 描述 |
| --- | --- |
| `$table->primary('id');` | 添加一個主鍵。 |
| `$table->primary(['first', 'last']);` | 添加組合鍵。 |
| `$table->unique('email');` | 添加唯一索引。 |
| `$table->index('state');` | 添加基本??索引。 |
### [](https://octobercms.com/docs/database/structure#dropping-indexes)刪除索引
要刪除索引,必須指定索引的名稱。如果沒有手動指定名稱,則系統將自動生成一個名稱,只需將表名稱,索引列的名稱和索引類型連接在一起。這里有些例子:
| 命令 | 描述 |
| --- | --- |
| `$table->dropPrimary('users_id_primary');` | 從“用戶”表中刪除主鍵。 |
| `$table->dropUnique('users_email_unique');` | 從“用戶”表中刪除唯一索引。 |
| `$table->dropIndex('geo_state_index');` | 從“ geo”表中刪除基本索引。 |
### [](https://octobercms.com/docs/database/structure#foreign-key-constraints)外鍵約束
還支持創建外鍵約束,這些約束用于在數據庫級別強制引用完整性。例如,讓我們`user_id`在`posts`表上定義一個列,該`id`列引用表上的列`users`:
~~~
Schema::table('posts', function ($table) {
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
~~~
和以前一樣,您可以通過將第二個參數傳遞給`foreign`方法來手動指定約束的名稱:
~~~
$table->foreign('user_id', 'user_foreign')
->references('id')
->on('users');
~~~
您還可以為約束的“刪除時”和“更新時”屬性指定所需的操作:
~~~
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
~~~
要刪除外鍵,可以使用`dropForeign`方法。外鍵約束使用與索引相同的命名約定。因此,如果未手動指定,我們將連接表名和約束中的列,然后在名稱后綴“ \_foreign”:
~~~
$table->dropForeign('posts_user_id_foreign');
~~~
### [](https://octobercms.com/docs/database/structure#seeder-structure)播種者結構
像遷移文件一樣,默認情況下,種子類僅包含一個方法:`run`并且應該擴展`Seeder`該類。`run`執行更新過程時將調用該方法。在這種方法中,您可以根據需要將數據插入數據庫中。您可以使用[查詢生成器](https://octobercms.com/docs/database/query)手動插入數據,也可以使用[模型類](https://octobercms.com/docs/database/model)。在下面的示例中,我們將使用方法`User`內的模型創建一個新用戶`run`:
~~~
<?php namespace Acme\Users\Updates;
use Seeder;
use Acme\Users\Models\User;
class SeedUsersTable extends Seeder
{
public function run()
{
$user = User::create([
'email' => 'user@example.com',
'login' => 'user',
'password' => 'password123',
'password_confirmation' => 'password123',
'first_name' => 'Actual',
'last_name' => 'Person',
'is_activated' => true
]);
}
}
~~~
另外,也可以使用`Db::table`[查詢生成器](https://octobercms.com/docs/database/query)方法實現相同的目的:
~~~
public function run()
{
$user = Db::table('users')->insert([
'email' => 'user@example.com',
'login' => 'user',
[...]
]);
}
~~~
### [](https://octobercms.com/docs/database/structure#calling-additional-seeders)呼叫其他播種機
在`DatabaseSeeder`該類中,您可以使用該`call`方法執行其他種子類。使用該`call`方法可以將數據庫種子分解為多個文件,從而使單個種子類都不會變得太大。只需傳遞您希望運行的種子類的名稱即可:
~~~
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
$this->call('Acme\Users\Updates\UserTableSeeder');
$this->call('Acme\Users\Updates\PostsTableSeeder');
$this->call('Acme\Users\Updates\CommentsTableSeeder');
}
~~~
- 基本說明
- 基本操作
- October cms 安裝
- 后臺控制器路徑
- 圖標
- 獲取安裝網上的插件/主題
- 插件構造器使用
- 定時任務
- October后臺控制器
- vscode編輯器
- ajax操作
- 使用
- ajax更新組件
- ajax屬性API
- JavaScript API
- ajax綜合使用
- 主題
- 多語言主題
- 安裝市場主題
- 主題程序處理
- 主題
- 頁面
- 部件
- 布局
- 內容
- 組件
- 媒體
- 主題表單操作
- 表單使用
- 表單后端程序處理
- 插件
- 自定義插件
- 插件說明
- 插件導航條
- 插件數據庫設置
- 插件的設置管理
- 插件的配置文件config
- 組件
- app服務
- app容器
- 擴展行為
- 緩存
- Collection類
- Lazy Collections
- Collection方法
- 助手函數
- 數組助手函數
- 路徑助手函數
- 玄樂助手函數
- 其他助手函數
- 錯誤與記錄
- 事件處理
- HTML頁面
- 文件與目錄操作
- 散列和加密
- 郵件
- 郵件內容
- 郵件發送
- 分頁
- 模板解析器
- 動態解析器語法
- 隊列消息
- 請求與輸入
- 響應
- 視圖
- 路由器
- 配置
- 驗證操作
- 處理錯誤消息
- 錯誤消息與視圖
- 可用的驗證規則
- 有條件的驗證規則
- 驗證數組
- 錯誤消息
- 自定義驗證規則
- 模型操作
- 定義模型與其屬性
- 檢索模型
- 插入與更新
- 刪除模型
- 查詢范圍
- 事件操作
- 關聯操作
- 定義關系
- 關系類型
- 多肽關系
- 關系查詢
- 渴望加載
- 插入模型
- 數據庫操作
- 基本用法
- 數據表結構
- 查詢連貫操作
- 結果檢索
- select子句
- 插入更新
- where子句
- 排序,分組,限制和偏移
- 文件附件
- Collection操作
- 屬性操作
- 系列化json
- 數據庫屬性
- 數據庫行為
- 控制器
- 后臺控制器定義
- 后臺頁面
- 后臺組件
- 后臺表單
- 表單組件
- 表單視圖
- 表單行為
- 后臺列表
- 列表行為
- 列表過濾器
- 可用列類型
- 關系行為
- 關系行為類型
- 擴展關系行為
- 列表排序操作
- 導入導出操作
- 用于與權限
- corlate模板修改
- 修改頂部導航
- laravel問題
- 控制器不存在
- 控制器
- 路由組
- laravel筆記
- laravel 安裝
- 偽靜態配置
- 依賴注入 & 控制器
- 中間件
- 路由文件
- 視圖