🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[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` 字段。