一:创建迁移在laravel中使用make:migration命令来创建迁移php artisan make:migration create_user_table执行上面的命令后这时候会在datab
顺晟科技
2023-02-26 09:49:50
148
使用
laravel上的make:migration命令创建迁移
PHP artisan make:migration create _ user _ table上述命令将在database/migrations目录中创建相应的迁移文件。每次迁移的文件名都包含时间戳,以便Laravel可以确定迁移顺序
迁移类包含两种方法:up和down。Up方法用于添加新数据库的数据表、字段或索引,而down方法必须与up方法的执行相反。
public function up()
{
schema:create(& amp;# 039;user & amp# 039;function (blueprint $ table) {
$ table-bigIncrements(& amp;# 039;id & amp# 039;);
$ table-string(& amp;# 039;name & amp# 039;);
$ table-string(& amp;# 039;电子邮件和amp# 039;)-unique();
$ table-timestamp(& amp;# 039;email _ verified _ at & amp# 039;)-nullable();
$ table-string(& amp;# 039;password & amp# 039;);
$ table-rememberToken();
$ table-timestamps();
});
}
public function down()
{
模式:dropifexists(& amp;# 039;user & amp# 039;);
}
PHP artisan migrate大多数迁移工作都是破坏性的。这意味着可能会丢失数据。为了防止有人在生产环境数据中执行这些命令,请在执行这些命令之前显示确认消息。要在没有提示的情况下强制执行迁移命令,请使用- force参数
通过在PHP artisan migrate-force
PHP artisan migrate:roll back roll back命令中添加step参数,可以回滚指定数量的迁移
PHP artisan migrate:roll back-step=5 migrate:reset命令可回滚应用程序的所有迁移。
PHP artisan migrate:reset
migrate:refresh命令在回滚所有迁移后运行migrate命令。此命令可以有效地重新创建整个数据库。
Php artisan迁移:refresh
//刷新数据库并执行数据库填充
PHP artisan migrate:refresh-seed
laravel的数据库迁移支持以下字段类型:
命令说明$ table-bigin crements(& amp;# 039;id & amp# 039;);增量ID(主键)为& ampquotunsigned big integer & ampquot$ table-big integer(& amp;# 039;votes & amp# 039;);BIGINT $ table-binary(& amp;# 039;data & amp# 039;)。BLOB $ table-boolean(& amp;# 039;confirmed & amp# 039;)。BOOLEAN $ table-char(& amp;# 039;name & amp# 039;100)。具有长度的CHAR $ table-date(& amp;# 039;created _ at & amp# 039;);date $ table-datetime(& amp;# 039;created _ at & amp# 039;)。datetime $ table-datetimetz(& amp;# 039;created _ at & amp# 039;)。时区datetime $ table-decimal(& amp;# 039;amount & amp# 039;8,2)。具有精度和基数的decimal $ table-double(& amp;# 039;amount & amp# 039;8,2)。具有精度和基数的double $ table-enum(& amp;# 039;level & amp# 039;[& amp;# 039;easy & amp# 039;& amp# 039;hard & amp# 039;]);ENUM $ table-float(& amp;# 039;amount & amp# 039;8,2)。具有精度和基数的float $ table-geometry(& amp;# 039;positions & amp# 039;);Geometry $ table-geometry集合(& amp# 039;positions & amp# 039;)。geometry collection $ table-increments(& amp;# 039;id & amp# 039;)。& ampquotunsignedinteger & ampquot$ table-integer(& amp;# 039;votes & amp# 039;)的增量ID(主键);integer $ table-IP address(& amp;# 039;visitor & amp# 039;)。IP地址$ table-JSON(& amp;# 039;options & amp# 039;)。JSON $ table-jsonb(& amp;# 039;选项& amp# 039;)。jsonb $ table-linestring(& amp;# 039;positions & amp# 039;)。linestring $ table-long text(& amp;# 039;description & amp# 039;)。long text $ table-MAC address(& amp;# 039;device & amp# 039;)。MAC地址$ table-medium increments(& amp;# 039;id & amp# 039;)。增量ID(主键)为& ampquotunsigned medium integer & ampquot$ table-medium integer(& amp;# 039;votes & amp# 039;);mediumint $ table-medium text(& amp;# 039;description & amp# 039;)。medium text $ table-morphs(& amp;# 039;taggable & amp# 039;);增量taggable_id和字符串taggable _ type $ table-uuid morphs(& amp;# 039;taggable & amp# 039;)。这等效于添加Taggable_id和字符串taggable _ typeUUID列。$ table-multiline string(& amp;# 039;positions & amp# 039;);multilinestring $ table-multi point(& amp;# 039;positions & amp# 039;)。multipoint $ table-multipolygon(& amp;# 039;positions & amp# 039;)。multipolygon $ table-nullable morphs(& amp;# 039;taggable & amp# 039;)。空版本的morphs()字段$ table-nullableuidmorphs(& amp;# 039;taggable & amp# 039;)。UuidMorphs()字段$ table-nullableTimestamps();空版本的timestamps()字段$ table-point(& amp;# 039;position & amp# 039;)。POINT $ table-polygon(& amp;# 039;positions & amp# 039;)。POLYGON $ table-rememberToken();与VARCHAR (100)的可空版本相对应的remember_token字段$ table-set(& amp;# 039;flavors & amp# 039;[& amp;# 039;strawberry & amp# 039;& amp# 039;vanilla & amp# 039;)SET $ table-small increments(& amp;# 039;id & amp# 039;)。增量ID(主键)为& ampquotunsigned smallint & ampquot$ table-small integer(& amp;# 039;votes & amp# 039;);SMALLINT $ table-soft deletes();这等效于添加空的deleted_at字段$table-softDeletesTz()进行软删除。具有空时区的deleted_at字段$ table-string(& amp;# 039;name & amp# 039;100)。具有长度的varchar $ table-text(& amp;# 039;description & amp# 039;);TEXT $ table-time(& amp;# 039;sunrise & amp# 039;)。TIME $ table-timeTz(& amp;# 039;sunrise & amp# 039;)。具有时区的TIME $ table-timestamp(& amp;# 039;added _ on & amp# 039;)。timestamp $ table-timestamptz(& amp;# 039;added _ on & amp# 039;)。具有时区的TIMESTAMP $ table-timestamps();空的created_at和updated _ at timestamp $ table-timestamps tz();created_at和updated _ at timestamp $ table-tiny increments(& amp;# 039;id & amp# 039;);unsigned tinyint $ table-tiny integer(& amp;# 039;votes & amp# 039;)相当于自动增加。tinyint $ table-unsigned biginteger(& amp;# 039;votes & amp# 039;)。unsigned bigint $ table-unsigned decimal(& amp;# 039;amount & amp# 039;8,2)。具有精度和基数的unsigned decimal $ table-unsigned integer(& amp;# 039;votes & amp# 039;)。unsigned int $ table-unsigned mediuminteger(& amp;# 039;votes & amp# 039;)。unsigned mediumint $ table-unsignedsmallinteger(& amp;# 039;votes & amp# 039;)。unsigneds mallint $ table-unsignedtinyinteger(& amp;# 039;votes & amp# 039;)。unsigned TINYINT $ table-uuid(& amp;# 039;id & amp# 039;)。UUID $ table-year(& amp;# 039;birth _ year & amp# 039;);YEAR
等于laravel的数据库迁移支持以下字段修改器:
命令说明-after(& amp;# 039;column & amp# 039;)此字段为其他字段& amp# 039;放在后面& amp# 039;(MySQL)-自动增加autoIncrement () INTEGER类型字段的键-charset(& amp;# 039;指定列排序规则(MySQL/SQL server)-comment(& amp;# 039;my comment & amp# 039;)字段中添加注释(MySQL)-在default($value)字段中添加& amp# 039;指定默认值使用此字段时,设置NULL值(默认值)-storedAs($expression)存储生成字段创建(MySQL)-unsigned () INTEGER类型的字段为unsigned (MySQL)使用virtual as ($ expression)创建虚拟创建字段(MySQL)-generatedAs($expression)。序列值定义用于标识列的输入(PostgreSQL)-primara# 039;id & amp# 039;)键-主([& amp;# 039;id & amp# 039;Parent _ id & amp# 039;])添加复合密钥-unique(& amp;# 039;电子邮件和amp# 039;)添加唯一索引-index(& amp;# 039;state & amp# 039;)添加常规索引-空间索引(& amp# 039;ll To & amp# 039;)重命名索引-drop primary(& amp;# 039;users _ id _ primary & amp# 039;)删除密钥-drop unique(& amp;# 039;删除users _ email _ unique唯一索引-drop index(& amp;# 039;geo _ state _ index & amp# 039;);删除默认索引-dropspatialindex(& amp;# 039;geo _ location _ spatial index & amp# 039;);要删除空间索引(不支持SQLite)实例,请执行以下操作:
schema:table(& amp;# 039;users & amp# 039;function (blueprint $ table) {
$ table-string(& amp;# 039;电子邮件和amp# 039;)-nullable();
});
change方法将现有字段类型更改为新类型或修改属性。例如:
schema:table(& amp;# 039;users & amp# 039;function (blueprint $ table) {
$ table-string(& amp;# 039;name & amp# 039;50)-change();
});RenameColumn方法根据doctrine/dbal扩展重命名字段。例如:
schema:table(& amp;# 039;users & amp# 039;function (blueprint $ table) {
$ table-rename column(& amp;# 039;from & amp# 039;& amp# 039;to & amp# 039;);
});使用
dropColumn方法删除字段。例如:
schema:table(& amp;# 039;users & amp# 039;function (blueprint $ table) {
$ table-drop column([& amp;# 039;votes & amp# 039;& amp# 039;avatar & amp# 039;& amp# 039;location & amp# 039;]);删除//votes、avatar和location字段
});
Laravel默认使用utf8mb4编码,并支持在数据库中存储emojis。如果在5.7.7之前版本的MySQL或10.2.2之前版本的MariaDB中创建索引,则必须手动配置数据库迁移的默认字符串长度。也就是说,通过从app/providers/appservice provider调用模式:defaultstringlength方法来配置该方法
use illuminate \ support \ facades \ schema;
/* *
* boot strap any application services。
*
* @return void
*/
Public function boot()
{
模式:defaultstringlength(191);
}
Laravel还支持在数据库层创建强制引用完整性的外键约束条件。例如,让我们定义一个user_id字段,该字段在posts表中引用users表中的id字段。
schema:table(& amp;# 039;posts & amp# 039;function (blueprint $ table) {
$ table-unsigned biginteger(& amp;# 039;user _ id & amp# 039;);
$ table-foreign(& amp;# 039;user _ id & amp# 039;)-references(& amp;# 039;id & amp# 039;)-on(& amp;# 039;users & amp# 039;);
});在迁移文档中,使用以下方法打开或关闭外键约束
模式:enableforeignkeyconstraints();
模式:disableforeignkeyconstraints();