Laravel 的数据库迁移的方法

时间:2017-08-11

Modifier Description
->after('column') 将此字段放置在其它字段「之后」(仅限 MySQL)
->comment('my comment') 增加注释
->default($value) 为此字段指定「默认」值
->first() 将此字段放置在数据表的「首位」(仅限 MySQL)
->nullable() 此字段允许写入 NULL 值
->storedAs($expression) 创建一个存储的生成字段 (仅限 MySQL)
->unsigned() 设置 integer 字段为 UNSIGNED
->virtualAs($expression) 创建一个虚拟的生成字段 (仅限 MySQL)

字段更新

Schema::table('users', function (Blueprint $table) {
  $table->string('phone',20)->change();
  $table->string('username',60)->->nullable()->change();
});

重命名字段

Schema::table('users', function (Blueprint $table) {
  $table->renameColumn('from', 'to');
});

字段移除

Schema::table('users', function (Blueprint $table) {
  $table->dropColumn(['last_ip', 'last_login']);
});

在使用字段更新,重命名字段,字段移除之前,请务必在你的 composer.json文件require键名中添加< "doctrine/dbal": "^2.5">值。然后composer update进行更新或

composer require doctrine/dbal

创建索引

$table->string('email')->unique();

Command Description
$table->primary('id'); 加入主键。
$table->primary(['first', 'last']); 加入复合键。
$table->unique('email'); 加入唯一索引。
$table->unique('state', 'my_index_name'); 自定义索引名称。
$table->unique(['first', 'last']); 加入复合唯一键。
$table->index('state'); 加入基本索引。

开启和关闭外键约束

Schema::enableForeignKeyConstraints();
Schema::disableForeignKeyConstraints();

运行迁移

php artisan migrate

在线上环境强制执行迁移

php artisan migrate --force

回滚迁移

若要回滚最后一次迁移,则可以使用 rollback 命令。此命令是对上一次执行的「批量」迁移回滚,其中可能包括多个迁移文件:

php artisan migrate:rollback
  • 共4页:
  • 上一页
  • 3/4
  • 下一页
  • 上一篇:php中文乱码问题的终极解决方案汇总 下一篇:PHP实现webshell扫描文件木马的方法

    相关文章

    最新文章