1. 介绍
迁移的作用就是编程式的创建应用数据库的结构
2. 创建迁移
php artisan make:migration create_users_table
新创建的迁移会放在 database/migrations 目录 ,如 2014_10_12_000000_create_users_table.php
文件名中的时间字段用来让 Laravel 确认迁移顺序 ,即 up 时按时间升序执行 ,down 时按降序执行
| 选项 |
说明 |
| --create |
指定迁移表的名称 |
| --table |
迁移时是否创建新数据表 |
3. 迁移结构
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
迁移类中有两个方法 up() 和 down()
| 方法 |
说明 |
| up() |
新增数据库中的数据表 、字段 、索引等 |
| down() |
down() 方法应该与 up() 执行的操作相反 ,即回滚 |
4. 表操作
4.1 创建表
Scheam::create('tablename', function(Blueprint $table){
$table->engine = 'InnoDB';
$table->charset = 'utf8';
$table->collation = 'utf8_unicode_ci';
$table->temporary();
});
4.2 操作表
Schema::rename('old_table_name', 'new_table_name');
Schema::drop('table_name');
Schema::dropIfExists('table_name');
5. 列操作
Scheam::create('tablename', function(Blueprint $table){
$table->string('name');
});
5.1 整型列
| 方法 |
说明 |
| integer(colName) |
添加 int 列 |
| tinyInteger(colName) |
添加 tinyint 列 |
| smallInteger(colName) |
添加 smallint 列 |
| mediumInteger(colName) |
添加 mediumint 列 |
| bigInteger(colName) |
添加 bigint 列 |
5.2 文本列
| 方法 |
说明 |
| string(colName, length) |
添加 VARCAHR 列 |
| char(colName, length) |
添加 CHAR 列 |
| text(colName) |
添加 TEXT 列 |
| mediumText(colName) |
添加 MEDIUMTEXT 列 |
| longText(colName) |
添加 LONGTEXT 列 |
5.3 二进制列
| 方法 |
说明 |
| binary(colName) |
添加 BLOB 列 |
5.4 布尔列
| 方法 |
说明 |
| boolean(colName) |
添加 BOOLEAN 列( MySQL 中是 TINYINT(1) ) |
5.5 时间列
| 方法 |
说明 |
| datetime(colName) |
添加 DATETIME 列 |
| time(colName) |
添加 TIME 列 |
| timestamp(colName) |
添加 TIMESTAMP 列 |
| timestamps() |
添加名为 created_at 的时间戳列 |
| nullableTimestamps() |
添加名为 updated_at 的时间戳列 |
5.6 浮点型列
| 方法 |
说明 |
| decimal(colName, precision, scale) |
添加 DECIMAL 列 |
| double(colName, disits, decimal) |
添加 DOUBLE 列 |
| float(colName) |
添加 FLOAT 列 |
5.7 枚举列
| 方法 |
说明 |
| enum(colName, [choiceOne, choiceTwo]) |
添加 ENUM 列 |
5.8 主键列
| 方法 |
说明 |
| increments(colName) |
添加无符号 INT 型自增主键 |
| bigIncrements(colName) |
添加无符号 LONGINT 型自增主键 |
5.9 其他列
| 方法 |
说明 |
| uuid(colName) |
添加 UUID 列( MySQL 中是 CHAR(36) ) |
| rememberToken() |
为用户 "remember me" 的令牌添加一个 remember_token 类型列( VARCHAR(100) ) |
| softDeletes() |
为包含软删除的用户添加一个 deleted_at 时间戳 |
6. 设置列的属性
| 方法 |
说明 |
| nullable() |
允许在列中插入 NULL |
| default('default value') |
默认值 |
| unsigned() |
无符号 |
| first() |
按列顺序将列放到首列( 仅支持 MySQL ) |
| after(colName) |
按列顺序将当前列放到 colName 列后面 |
| unique() |
添加 UNIQUE 索引 |
| primary() |
添加主键索引 |
| index() |
添加基本索引 |
| comment('comment') |
添加注释 |
7. 修改列
修改列之前请确保将 doctrine/dbal 依赖添加到 composer.json 文件中
composer require doctrine/dbal
Schema::table('users', function ($table) {
$table->string('name', 100)->change();
$table->string('email')->nullable()->change();
$table->renameColumn('promoted', 'is_promoted');
$table->dropColumn('votes');
});
8. 索引和外键
8.1 添加索引
$table->primary('id');
$table->primary(['frisr_name', 'last_name']);
$table->unique('email');
$table->index('amount');
8.2 删除索引
$table->dropPrimary('id');
$table->dropUnique('email');
$table->dropIndex('amount');
$table->dropIndex(['emial', 'amount']);
8.3 外键
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->dropForeign('contacts_user_id_foreign');
9. 运行迁移
| 命令 |
说明 |
| migrate |
运行所有迁移 |
| migrate:rollback |
回滚上一次的迁移 |
| migrate:reset |
回滚所有迁移 |
| migrate:refresh |
回滚所有迁移并重新运行所有迁移 |
| migrate:fresh |
删除所有表并重新运行所有迁移 |
| migrate:status |
显示每次迁移的状态 |
| migrate:install |
创建迁移存储库 |
migrate:rollback --step=2
migrate --path=/database/migrations/filename