加入字段
更新现有的数据表,可使用 Schema::table
方法:
Schema::table("users", function($table)
{
$table->string("email");
});
数据表产生器提供多种字段类型可使用,在您建立数据表时也许会用到:
命令 | 功能描述 |
---|---|
$table->bigIncrements("id"); | ID 自动增量,使用相当于「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->decimal("amount", 5, 2); | 相当于 DECIMAL 类型,并带有精度与基数 |
$table->double("column", 15, 8); | 相当于 DOUBLE 类型,总共有 15 位数,在小数点后面有 8 位数 |
$table->enum("choices", array("foo", "bar")); | 相当于 ENUM 类型 |
$table->float("amount"); | 相当于 FLOAT 类型 |
$table->increments("id"); | 相当于 Incrementing 类型 (数据表主键) |
$table->integer("votes"); | 相当于 INTEGER 类型 |
$table->json("options"); | 相当于 JSON 类型 |
$table->jsonb("options"); | JSONB equivalent to the table |
$table->longText("description"); | 相当于 LONGTEXT 类型 |
$table->mediumInteger("numbers"); | 相当于 MEDIUMINT 类型 |
$table->mediumText("description"); | 相当于 MEDIUMTEXT 类型 |
$table->morphs("taggable"); | 加入整数 taggable_id 与字串 taggable_type |
$table->nullableTimestamps(); | 与 timestamps() 相同,但允许 NULL |
$table->smallInteger("votes"); | 相当于 SMALLINT 类型 |
$table->tinyInteger("numbers"); | 相当于 TINYINT 类型 |
$table->softDeletes(); | 加入 deleted_at 字段于软删除使用 |
$table->string("email"); | 相当于 VARCHAR 类型 |
$table->string("name", 100); | 相当于 VARCHAR 类型,并指定长度 |
$table->text("description"); | 相当于 TEXT 类型 |
$table->time("sunrise"); | 相当于 TIME 类型 |
$table->timestamp("added_on"); | 相当于 TIMESTAMP 类型 |
$table->timestamps(); | 加入 created_at 和 updated_at 字段 |
$table->rememberToken(); | 加入 remember_token 使用 VARCHAR(100) NULL |
->nullable() | 标示此字段允许 NULL |
->default($value) | 声明此字段的默认值 |
->unsigned() | 配置整数是无分正负 |
在 MySQL 使用 After 方法
若您使用 MySQL 数据库,您可以使用 after 方法来指定字段的顺序:
$table->string("name")->after("email");