本文介绍了laravel 的数据库迁移的方法,分享给大家,具体如下:
生成迁移
--table 和 --create 选项可用来指定数据表的名称,或是该迁移被执行时会创建的新数据表。这些选项需在预生成迁移文件时填入指定的数据表:
添加字段
databasemigrations2017_07_30_133748_create_users_table.php
要创建一张新的数据表,可以使用 schema facade 的 create 方法。create 方法接收两个参数:第一个参数为数据表的名称,第二个参数为一个 闭包 ,此闭包会接收一个用于定义新数据表的 blueprint 对象
你可以方便地使用 hastable 和 hascolumn 方法来检查数据表或字段是否存在:
如果你想要在一个非默认的数据库连接中进行数据库结构操作,可以使用 connection 方法:
你可以在数据库结构构造器上设置 engine 属性来设置数据表的存储引擎:
重命名与删除数据表
创建字段
| $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 型态。 |
字段修饰
| ->after('column') | 将此字段放置在其它字段「之后」(仅限 mysql) |
| ->comment('my comment') | 增加注释 |
| ->default($value) | 为此字段指定「默认」值 |
| ->first() | 将此字段放置在数据表的「首位」(仅限 mysql) |
| ->nullable() | 此字段允许写入 null 值 |
| ->storedas($expression) | 创建一个存储的生成字段 (仅限 mysql) |
| ->unsigned() | 设置 integer 字段为 unsigned |
| ->virtualas($expression) | 创建一个虚拟的生成字段 (仅限 mysql) |
字段更新
重命名字段
字段移除
在使用字段更新,重命名字段,字段移除之前,请务必在你的 composer.json文件require键名中添加< "doctrine/dbal": "^2.5">值。然后composer update进行更新或
创建索引
| $table->primary('id'); | 加入主键。 |
| $table->primary(['first', 'last']); | 加入复合键。 |
| $table->unique('email'); | 加入唯一索引。 |
| $table->unique('state', 'my_index_name'); | 自定义索引名称。 |
| $table->unique(['first', 'last']); | 加入复合唯一键。 |
| $table->index('state'); | 加入基本索引。 |
开启和关闭外键约束
运行迁移
在线上环境强制执行迁移
回滚迁移
若要回滚最后一次迁移,则可以使用 rollback 命令。此命令是对上一次执行的「批量」迁移回滚,其中可能包括多个迁移文件:
在 rollback 命令后加上 step 参数,你可以限制回滚迁移的个数。例如,下面的命令将会回滚最后的 5 个迁移。
migrate:reset 命令可以回滚应用程序中的所有迁移:
使用单个命令来执行回滚或迁移
migrate:refresh 命令不仅会回滚数据库的所有迁移还会接着运行 migrate 命令。所以此命令可以有效的重新创建整个数据库:
使用 refresh 命令并加上 step 参数,你也可以限制执行回滚和再迁移的个数。比如,下面的命令会回滚并再迁移最后的 5 个迁移:
无法生成迁移文件
在 laravel 项目中,由于测试,有时候用 php artisan make:migration create_xxx_table 创建数据库迁移。如果把创建的迁移文件 database/migrations/2017_07_30_133748_create_xxx_table.php 文件给删除了,再次执行 php artisan make:migration create_xxx_table 会报错:
[errorexception]
include(e:laravervendorcomposer/../../database/migrations/2017_07_30_133748_create_users_table.php): failed to open stream: no such file or directory
重新运行 composer update 又可以执行上面的命令了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!