laravel 数据迁移

发布时间 2023-12-22 16:26:32作者: 『學無止境』

迁移命令
运行迁移: php artisan migrate
回滚最近一次数据迁移 :php artisan migrate:rollback
创建表: php artisan make:migration create_表名_table
新增字段: php artisan make:migration add_要添加的字段名_to_要添加字段的表名_table
修改字段: php artisan make:migration alter_表名_table
数据填充: php artisan db:seed --class SystemSettingSeeder

支持的字段类型:

命令 描述
$table->bigIncrements('id'); 递增 ID(主键),相当于「UNSIGNED BIG INTEGER」
$table->bigInteger('votes'); 相当于 BIGINT
$table->binary('data'); 相当于 BLOB
$table->boolean('confirmed'); 相当于 BOOLEAN
$table->char('name', 100); 相当于带有长度的 CHAR
$table->date('created_at'); 相当于 DATE
$table->dateTime('created_at'); 相当于 DATETIME
$table->dateTimeTz('created_at'); 相当于带时区 DATETIME
$table->decimal('amount', 8, 2); 相当于带有精度与基数 DECIMAL
$table->double('amount', 8, 2); 相当于带有精度与基数 DOUBLE
$table->enum('level', ['easy', 'hard']); 相当于 ENUM
$table->float('amount', 8, 2); 相当于带有精度与基数 FLOAT
$table->geometry('positions'); 相当于 GEOMETRY
$table->geometryCollection('positions'); 相当于 GEOMETRYCOLLECTION
$table->increments('id'); 递增的 ID (主键),相当于「UNSIGNED INTEGER」
$table->integer('votes'); 相当于 INTEGER
$table->ipAddress('visitor'); 相当于 IP 地址
$table->json('options'); 相当于 JSON
$table->jsonb('options'); 相当于 JSONB
$table->lineString('positions'); 相当于 LINESTRING
$table->longText('description'); 相当于 LONGTEXT
$table->macAddress('device'); 相当于 MAC 地址
$table->mediumIncrements('id'); 递增 ID (主键) ,相当于「UNSIGNED MEDIUM INTEGER」
$table->mediumInteger('votes'); 相当于 MEDIUMINT
$table->mediumText('description'); 相当于 MEDIUMTEXT
$table->morphs('taggable'); 相当于加入递增的 taggable_id 与字符串 taggable_type
$table->uuidMorphs('taggable'); 相当于加入 taggable_id 与字符串 taggable_typeUUID 列。
$table->multiLineString('positions'); 相当于 MULTILINESTRING
$table->multiPoint('positions'); 相当于 MULTIPOINT
$table->multiPolygon('positions'); 相当于 MULTIPOLYGON
$table->nullableMorphs('taggable'); 相当于可空版本的 morphs () 字段
$table->nullableUuidMorphs('taggable'); 相当于可空版本的 uuidMorphs() 字段
$table->nullableTimestamps(); 相当于可空版本的 timestamps() 字段
$table->point('position'); 相当于 POINT
$table->polygon('positions'); 相当于 POLYGON
$table->rememberToken(); 相当于可空版本的 VARCHAR (100) 的 remember_token 字段
$table->set('flavors', ['strawberry', 'vanilla']); 相当于 SET
$table->smallIncrements('id'); 递增 ID(主键),相当于「UNSIGNED SMALLINT」
$table->smallInteger('votes'); 相当于 SMALLINT
$table->softDeletes(); 相当于为软删除添加一个可空的 deleted_at 字段
$table->softDeletesTz(); 相当于为软删除添加一个可空的 带时区的 deleted_at 字段
$table->string('name', 100); 相当于带长度的 VARCHAR
$table->text('description'); 相当于 TEXT
$table->time('sunrise'); 相当于 TIME
$table->timeTz('sunrise'); 相当于带时区的 TIME
$table->timestamp('added_on'); 相当于 TIMESTAMP
$table->timestampTz('added_on'); 相当于带时区的 TIMESTAMP
$table->timestamps(); 相当于可空的 created_at 和 updated_at TIMESTAMP
$table->timestampsTz(); 相当于可空且带时区的 created_at 和 updated_at TIMESTAMP
$table->tinyIncrements('id'); 相当于自动递增 UNSIGNED TINYINT
$table->tinyInteger('votes'); 相当于 TINYINT
$table->unsignedBigInteger('votes'); 相当于 Unsigned BIGINT
$table->unsignedDecimal('amount', 8, 2); 相当于带有精度和基数的 UNSIGNED DECIMAL
$table->unsignedInteger('votes'); 相当于 Unsigned INT
$table->unsignedMediumInteger('votes'); 相当于 Unsigned MEDIUMINT
$table->unsignedSmallInteger('votes'); 相当于 Unsigned SMALLINT
$table->unsignedTinyInteger('votes'); 相当于 Unsigned TINYINT
$table->uuid('id'); 相当于 UUID
$table->year('birth_year'); 相当于 YEAR

支持的字段修饰符:

命令 描述
->after('column') 将此字段放置在其它字段 "之后" (MySQL)
->autoIncrement() 将 INTEGER 类型的字段设置为自动递增的主键
->charset('utf8') 指定一个字符集 (MySQL)
->collation('utf8_unicode_ci') 指定列的排序规则 (MySQL/SQL Server)
->comment('my comment') 为字段增加注释 (MySQL)
->default($value) 为字段指定 "默认" 值
->first() 将此字段放置在数据表的 "首位" (MySQL)
->nullable($value = true) 此字段允许写入 NULL 值(默认情况下)
->storedAs($expression) 创建一个存储生成的字段 (MySQL)
->unsigned() 设置 INTEGER 类型的字段为 UNSIGNED (MySQL)
->useCurrent() 将 TIMESTAMP 类型的字段设置为使用 CURRENT_TIMESTAMP 作为默认值
->virtualAs($expression) 创建一个虚拟生成的字段 (MySQL)
->generatedAs($expression) 使用指定的序列生成标识列(PostgreSQL)
->always() 定义序列值优先于标识列的输入 (PostgreSQL)
->primary('id') 添加主键
->primary(['id', 'parent_id']) 添加复合键
->unique('email') 添加唯一索引
->index('state') 添加普通索引
->spatialIndex('location') 添加空间索引(不支持 SQLite)
->renameIndex('from', 'to') 重命名索引
->dropPrimary('users_id_primary') 删除主键
->dropUnique('users_email_unique'); 删除唯一索引
->dropIndex('geo_state_index'); 删除基本索引
->dropSpatialIndex('geo_location_spatialindex'); 删除空间索引(不支持 SQLite)