[TOC]
## 字段創建
`CreateUsersTable` 類中通過 `Blueprint` 實例 `$table` 為 users 表創建所需的字段:
> 由 `increments` 方法創建一個` integer` 類型的自增長 id。
~~~php
$table->increments('id');
~~~
> 由 `string` 方法創建一個 `name` 字段,用于保存用戶名稱。
~~~php
$table->string('name');
~~~
> 由 `string` 方法創建一個 `email` 字段,且在最后指定該字段的值為唯一值,用于保存用戶郵箱。
~~~php
$table->string('email')->unique();
~~~
> 由 `string` 方法創建一個 `password` 字段,且在 `string` 方法中指定保存的值最大長度為 60,用于保存用戶密碼。
~~~php
$table->string('password', 60);
~~~
> 由 `rememberToken` 方法為用戶創建一個 `remember_token` 字段,用于保存『記住我』的相關信息。
~~~php
$table->rememberToken();
~~~
> 由 `timestamps` 方法創建了一個 `created_at` 和一個 `updated_at` 字段,分別用于保存用戶的創建時間和更新時間。
~~~php
$table->timestamps();
~~~
>[info] 該遷移文件最終生成的數據表如下:
字段名稱 | 字段類型
------------- | -------------
id | integer
name | string
email | string
password | string
remember_token | string
created_at | datetime
updated_at | datetime
### 可用的字段類型
命令 | 描述
------------- | -------------
`$table->bigIncrements('id');` | 遞增 ID(主鍵),相當于「UNSIGNED BIG INTEGER」型態。
`$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->dateTimeTz('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', 8, 2);` | 相當于 FLOAT 型態,總共有 8 位數,在小數點后面有 2 位數。
`$table->increments('id');` | 遞增的 ID (主鍵),使用相當于「UNSIGNED INTEGER」的型態。
`$table->integer('votes');` | 相當于 INTEGER 型態。
`$table->ipAddress('visitor');` | 相當于 IP 地址形態。
`$table->json('options');` | 相當于 JSON 型態。
`$table->jsonb('options');` | 相當于 JSONB 型態。
`$table->longText('description');` | 相當于 LONGTEXT 型態。
`$table->macAddress('device');` | 相當于 MAC 地址形態。
`$table->mediumIncrements('id');` | 遞增 ID (主鍵) ,相當于「UNSIGNED MEDIUM INTEGER」型態。
`$table->mediumInteger('numbers');` | 相當于 MEDIUMINT 型態。
`$table->mediumText('description');` | 相當于 MEDIUMTEXT 型態。
`$table->morphs('taggable');` | 加入整數 `taggable_id` 與字符串 `taggable_type`。
`$table->nullableMorphs('taggable');` | 與 `morphs()` 字段相同,但允許為NULL。
`$table->nullableTimestamps();` | 與 `timestamps()` 相同,但允許為 NULL。
`$table->rememberToken();` | 加入 `remember_token` 并使用 VARCHAR(100) NULL。
`$table->smallIncrements('id');` | 遞增 ID (主鍵) ,相當于「UNSIGNED SMALL INTEGER」型態。
`$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->timeTz('sunrise');` | 相當于 TIME (帶時區) 形態。
`$table->tinyInteger('numbers');` | 相當于 TINYINT 型態。
`$table->timestamp('added_on');` | 相當于 TIMESTAMP 型態。
`$table->timestampTz('added_on');` | 相當于 TIMESTAMP (帶時區) 形態。
`$table->timestamps();` | 加入 `created_at` 和 `updated_at` 字段。
`$table->timestampsTz();` | 加入 `created_at` and `updated_at` (帶時區) 字段,并允許為NULL。
`$table->unsignedBigInteger('votes');` | 相當于 Unsigned BIGINT 型態。
`$table->unsignedInteger('votes');` | 相當于 Unsigned INT 型態。
`$table->unsignedMediumInteger('votes');` | 相當于 Unsigned MEDIUMINT 型態。
`$table->unsignedSmallInteger('votes');` | 相當于 Unsigned SMALLINT 型態。
`$table->unsignedTinyInteger('votes');` | 相當于 Unsigned TINYINT 型態。
`$table->uuid('id');` | 相當于 UUID 型態。
### 可用的字段修飾
除了上述的字段類型列表,還有一些其它的字段「修飾」,你可以將它增加到字段中。
例如,若要讓字段「nullable」,那么你可以使用 `nullable` 方法:
~~~php
Schema::table('users', function (Blueprint $table) {
$table->string('email')->nullable();
});
~~~
修飾 | 描述
------------- | -------------
`->after('column')` | 將此字段放置在其它字段「之后」(僅限 MySQL)
`->comment('my comment')` | 增加注釋
`->default($value)` | 為此字段指定「默認」值
`->first()` | 將此字段放置在數據表的「首位」(僅限 MySQL)
`->nullable()` | 此字段允許寫入 NULL 值
`->storedAs($expression)` | 創建一個存儲的生成字段 (僅限 MySQL)
`->unsigned()` | 設置 `integer` 字段為 `UNSIGNED`
`->virtualAs($expression)` | 創建一個虛擬的生成字段 (僅限 MySQL)
>[warning] 此列表不包括 [索引修飾](database/indexes.md)
## 字段修改
### 先決條件
>[danger] 在修改字段之前,請**務必**在你的 `composer.json` 中增加 `doctrine/dbal` 依賴。
>[info] Doctrine DBAL 函數庫:
用來判斷當前字段的狀態,以及創建調整指定字段的 SQL 查詢。
~~~shell
$ composer require doctrine/dbal
// 或修改 composer.json 文件后執行
$ composer update
~~~
### 更改字段類型或屬性:實例 `$table` 的 `change` 方法
例子:
1. 增加字符串字段的長度,把 `name` 字段的長度從 25 增加到 50 :
~~~php
Schema::table('users', function (Blueprint $table) {
$table->string('name', 50)->change();
});
~~~
2. 將字段修改為 nullable:
~~~php
Schema::table('users', function (Blueprint $table) {
$table->string('name', 50)->nullable()->change();
});
~~~
>[danger] 下面的字段類型不能被「修改」:
char,double,enum,mediumInteger,timestamp,tinyInteger,ipAddress,json,jsonb,macAddress,mediumIncrements,morphs,nullableMorphs,nullableTimestamps,softDeletes,timeTz,timestampTz,timestamps,timestampsTz,unsignedMediumInteger,unsignedTinyInteger,uuid。
### 重命名字段:實例 `$table` 的 `renameColumn` 方法
~~~php
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('from', 'to');
});
~~~
### 刪除字段:實例 `$table` 的 `dropColumn` 方法
~~~php
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('votes');
});
~~~
#### 刪除多個字段:傳遞多個字段的數組至 `dropCloumn` 方法
~~~php
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['votes', 'avatar', 'location']);
});
~~~
>[warning] SQLite 數據庫不支持在單個遷移中刪除或修改多個字段。
#### 可用的刪除命令
命令 | 描述
--- | ---
`$table->dropRememberToken();` | 刪除 `remember_token` 字段。
`$table->dropSoftDeletes();` | 刪除用于軟刪除操作 `deleted_at` 字段。
`$table->dropSoftDeletesTz();` | 刪除 `deleted_at` 字段。
`$table->dropTimestamps();` | 刪除 `created_at` 和 `updated_at` 字段。
`$table->dropTimestampsTz();` | 刪除 `created_at` 和 `updated_at` 字段。
- 日常命令
- 第一章 開發環境部署
- 第一節 安裝 VirtualBox
- 第二節 安裝 Vagrant
- 第三節 安裝 Git for Windows
- 命令行提示符的寫作約定
- Git Bash 使用技巧
- 第四節 安裝和配置 Homestead
- 第五節 啟動 Homestead 虛擬機
- 第六節 創建 Laravel 應用
- 第七節 使用 Git 進行版本控制
- 總結 本章命令概覽
- 第二章 前端工作流
- 第一節 Yarn 安裝前端擴展包
- 第二節 Laravel Mix 前端資源編譯
- (1)配置 webpack.mix.js 指定 Mix 任務
- (2)修改 resources/assets 前端資源文件
- (3)運行 Mix 任務編譯和監控代碼
- 總結 本章命令概覽
- 第三章 數據庫
- 第一節 Config 數據庫配置信息
- 第二節 Migrations 數據庫遷移文件
- 第三節 Schema 數據庫結構生成器
- (1) 數據表操作
- (2) 字段操作
- (3) 索引操作
- 第四節 Seeding 數據填充
- 第五節 DB 數據庫查詢構建器
- (1)獲取結果集