可用的字段類型

字段類型

數(shù)據(jù)庫結(jié)構(gòu)生成器包含構(gòu)建表時可以指定的各種字段類型:

命令 描述
$table->bigIncrements('id'); 遞增 ID(主鍵),相當(dāng)于「UNSIGNED BIG INTEGER」
$table->bigInteger('votes'); 相當(dāng)于 BIGINT
$table->binary('data'); 相當(dāng)于 BLOB
$table->boolean('confirmed'); 相當(dāng)于 BOOLEAN
$table->char('name', 100); 相當(dāng)于帶有長度的 CHAR
$table->date('created_at'); 相當(dāng)于 DATE
$table->dateTime('created_at'); 相當(dāng)于 DATETIME
$table->dateTimeTz('created_at'); 相當(dāng)于帶時區(qū) DATETIME
$table->decimal('amount', 8, 2); 相當(dāng)于帶有精度與基數(shù) DECIMAL
$table->double('amount', 8, 2); 相當(dāng)于帶有精度與基數(shù) DOUBLE
$table->enum('level', ['easy', 'hard']); 相當(dāng)于 ENUM
$table->float('amount', 8, 2); 相當(dāng)于帶有精度與基數(shù) FLOAT
$table->geometry('positions'); 相當(dāng)于 GEOMETRY
$table->geometryCollection('positions'); 相當(dāng)于 GEOMETRYCOLLECTION
$table->increments('id'); 遞增的 ID (主鍵),相當(dāng)于「UNSIGNED INTEGER」
$table->integer('votes'); 相當(dāng)于 INTEGER
$table->ipAddress('visitor'); 相當(dāng)于 IP 地址
$table->json('options'); 相當(dāng)于 JSON
$table->jsonb('options'); 相當(dāng)于 JSONB
$table->lineString('positions'); 相當(dāng)于 LINESTRING
$table->longText('description'); 相當(dāng)于 LONGTEXT
$table->macAddress('device'); 相當(dāng)于 MAC 地址
$table->mediumIncrements('id'); 遞增 ID (主鍵) ,相當(dāng)于「UNSIGNED MEDIUM INTEGER」
$table->mediumInteger('votes'); 相當(dāng)于 MEDIUMINT
$table->mediumText('description'); 相當(dāng)于 MEDIUMTEXT
$table->morphs('taggable'); 相當(dāng)于加入遞增的 taggable_id 與字符串 taggable_type
$table->multiLineString('positions'); 相當(dāng)于 MULTILINESTRING
$table->multiPoint('positions'); 相當(dāng)于 MULTIPOINT
$table->multiPolygon('positions'); 相當(dāng)于 MULTIPOLYGON
$table->nullableMorphs('taggable'); 相當(dāng)于可空版本的 morphs() 字段
$table->nullableTimestamps(); 相當(dāng)于可空版本的 timestamps() 字段
$table->point('position'); 相當(dāng)于 POINT
$table->polygon('positions'); 相當(dāng)于 POLYGON
$table->rememberToken(); 相當(dāng)于可空版本的 VARCHAR(100) 的 remember_token 字段
$table->smallIncrements('id'); 遞增 ID (主鍵) ,相當(dāng)于「UNSIGNED SMALL INTEGER」
$table->smallInteger('votes'); 相當(dāng)于 SMALLINT
$table->softDeletes(); 相當(dāng)于為軟刪除添加一個可空的 deleted_at 字段
$table->softDeletesTz(); 相當(dāng)于為軟刪除添加一個可空的 帶時區(qū)的 deleted_at 字段
$table->string('name', 100); 相當(dāng)于帶長度的 VARCHAR
$table->text('description'); 相當(dāng)于 TEXT
$table->time('sunrise'); 相當(dāng)于 TIME
$table->timeTz('sunrise'); 相當(dāng)于帶時區(qū)的 TIME
$table->timestamp('added_on'); 相當(dāng)于 TIMESTAMP
$table->timestampTz('added_on'); 相當(dāng)于帶時區(qū)的 TIMESTAMP
$table->timestamps(); 相當(dāng)于可空的 created_atupdated_at TIMESTAMP
$table->timestampsTz(); 相當(dāng)于可空且?guī)r區(qū)的 created_atupdated_atTIMESTAMP
$table->tinyIncrements('id'); 相當(dāng)于自動遞增 UNSIGNED TINYINT
$table->tinyInteger('votes'); 相當(dāng)于 TINYINT
$table->unsignedBigInteger('votes'); 相當(dāng)于 Unsigned BIGINT
$table->unsignedDecimal('amount', 8, 2); 相當(dāng)于帶有精度和基數(shù)的 UNSIGNED DECIMAL
$table->unsignedInteger('votes'); 相當(dāng)于 Unsigned INT
$table->unsignedMediumInteger('votes'); 相當(dāng)于 Unsigned MEDIUMINT
$table->unsignedSmallInteger('votes'); 相當(dāng)于 Unsigned SMALLINT
$table->unsignedTinyInteger('votes'); 相當(dāng)于 Unsigned TINYINT
$table->uuid('id'); 相當(dāng)于 UUID
$table->year('birth_year'); 相當(dāng)于 YEAR

字段修飾

Modifier Description
->after('column') 將此字段放置在其它字段 "之后" (MySQL)
->autoIncrement() 將 INTEGER 類型的字段設(shè)置為自動遞增的主鍵
->charset('utf8') 指定一個字符集 (MySQL)
->collation('utf8_unicode_ci') 指定列的排序規(guī)則 (MySQL/SQL Server)
->comment('my comment') 為字段增加注釋 (MySQL)
->default($value) 為字段指定 "默認" 值
->first() 將此字段放置在數(shù)據(jù)表的 "首位" (MySQL)
->nullable($value = true) 此字段允許寫入 NULL 值(默認情況下)
->storedAs($expression) 創(chuàng)建一個存儲生成的字段 (MySQL)
->unsigned() 設(shè)置 INTEGER 類型的字段為 UNSIGNED (MySQL)
->useCurrent() 將 TIMESTAMP 類型的字段設(shè)置為使用 CURRENT_TIMESTAMP 作為默認值
->virtualAs($expression) 創(chuàng)建一個虛擬生成的字段 (MySQL)

可用的命令別名

Command Description
$table->dropRememberToken() 刪除 remember_token 字段。
$table->dropSoftDeletes() 刪除 deleted_at 字段。
$table->dropSoftDeletesTz() dropSoftDeletes() 方法的別名。
$table->dropTimestamps() 刪除 created_at and updated_at 字段。
$table->dropTimestampsTz() dropTimestamps() 方法的別名。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容