总的来说,做一次独立数据库迁移只需要三步,分别是创建迁移文件、修改迁移文件、运行迁移

1.创建数据库迁移文件
php artisan make:migration create_articles_table
1
然后在database/migrations 目录下就会多出一个迁移文件,不过Laravel会在前面自动加上时间戳,来判断执行顺序
然后命令后面可以加上–table 或者 –create 来指定迁移文件对应的表名或者是新建一个数据表

php artisan make:migration create_articles_table --create=article
php artisan make:migration create_articles_table --table=article
1
2
2.修改迁移文件
打开新建文件我们会发现里面包含了两个方法up和down

当运行迁移时,调用up 方法
当回滚迁移时,调用down 方法

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticlesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
我们主要修改up 方法,而Laravel已经为我们生成了两个字段,我们在上面添加两个字段

public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('content');
$table->timestamps();
});
}
1
2
3
4
5
6
7
8
9
3.运行迁移
php artisan migrate
1
Laravel会自动寻找没有执行的迁移文件,如果你只想执行指定文件的话,可以在后面加上–path参数,指定文件名

然后我们可以连接数据库,看到新建了一张名为article的表,同时里面包含了5个字段。

如果要回滚迁移的话
1.回滚所有迁移

php artisan migrate:reset
1
2.回滚最后一次执行的迁移

php artisan migrate:rollback
1
3.重置数据库并重新运行所有迁移

php artisan migrate:refresh

最新文章

  1. LINUX 下Open cv练习使用小记(2)
  2. centos6.5 ssh安全优化,修改默认端口名,禁止root远程登录
  3. 在完成一个异步任务后取消剩余任务(C#)
  4. Hadoop 学习之 FAQ
  5. Swift基础语法-内存管理, 自动引用计数
  6. 标准初始化css样式表
  7. SQL server 触发器、视图
  8. G - MPI Maelstrom
  9. Python之路,Day17 - 分分钟做个BBS论坛
  10. MATLAB获取“非免驱的相机或者摄像头”的图像数据
  11. mysql模糊匹配
  12. 了解Java基础原理
  13. Codeforces 897D. Ithea Plays With Chtholly (交互)
  14. 【Android 应用开发】AndroidUI设计 之 图片浏览器
  15. SpringCloud学习系列之五-----配置中心(Config)和消息总线(Bus)完美使用版
  16. windows的git的安装和配置
  17. DB2锁与隔离级别
  18. Struts2——通配符,Action Method_DMI
  19. Codeforces 873F Forbidden Indices 字符串 SAM/(SA+单调栈)
  20. itsdangerous

热门文章

  1. 纪中20日c组T2 2122. 【2016-12-31普及组模拟】幸运票
  2. mac本地安装全局包报错npm WARN checkPermissions
  3. cf1276B
  4. Vue自定义全局Toast和Loading
  5. C++中复制构造函数被调用的三种情况
  6. 剑指offer-面试题20-表示数值的字符串-字符串
  7. 谷歌浏览器chrome应用商店无法打开的解决方法
  8. PAT (Advanced Level) Practice 1152 Google Recruitment (20 分)
  9. 入门移动端混合开发 实战京东 APP(完整更新)
  10. Docker最全教程——从理论到实战(十九)