使用命令
php artisan make:migration table_name
其作用就是创建一个类:database\migrations\2017_08_08_130655_table_name.php
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class TableName extends Migration { /** * Run the migrations. * * @return void */ public function up() { // } /** * Reverse the migrations. * * @return void */ public function down() { // } }
一个迁移类会包含两个方法: up
和 down
。 up
方法可为数据库添加新的数据表、字段或索引,而 down
方法则是 up
方法的逆操作。
UP:创建数据表使用 Schema
Schema::create('table_name', function (Blueprint $table) {
//第一个参数为数据表名,第二个参数为一个
闭包
,此闭包会接收一个用于定义新数据表的
Blueprint
对象
});
Down:
删除已有的数据表:
Schema::drop('table_name');
表重命名:
Schema::rename('原名','新名');
创建字段:使用Blueprint
常用就是
$table->increments('id');
$table->string('name', 100);
$table->string('email')->unique();//不重复
$table->time('sunrise')->nullable()
;//可以为空
$table->timestamps();//加入
created_at
和
updated_at
字段
$table->integer('votes');
可用的字段类型
数据库结构构造器包含了许多字段类型,供你构建数据表时使用:
命令 | 描述 |
---|---|
$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
方法:
Schema::table('users', function (Blueprint $table) { $table->string('email')->nullable(); });
以下列表为字段的可用修饰。此列表不包括 索引修饰:
Modifier | Description |
---|---|
->after('column') | 将此字段放置在其它字段「之后」(仅限 MySQL) |
->comment('my comment') | 增加注释 |
->default($value) | 为此字段指定「默认」值 |
->first() | 将此字段放置在数据表的「首位」(仅限 MySQL) |
->nullable() | 此字段允许写入 NULL 值 |
->storedAs($expression) | 创建一个存储的生成字段 (仅限 MySQL) |
->unsigned() | 设置 integer 字段为 UNSIGNED |
->virtualAs($expression) | 创建一个虚拟的生成字段 (仅限 MySQL) |