laravel数据库迁移(三)
laravel号称世界上最好的框架,数据库迁移算上一个,在这里先简单入个门:
laravel很强大,它把表中的操作写成了migrations迁移文件,
然后可以直接通过迁移文件来操作表.
所以 , 数据迁移文件就是 操作表的语句文件 操作表的语句文件
为什么用迁移文件 , 而不直接敲 sql 操作表 ?
1. 便于团队统一操作表.
2. 出了问题,容易追查问题和回溯,有历史回退功能.
先创建一个库:

配置一下文件,修改.env中的参数

cmd.exe命令行输入如下命令创建一个表的迁移文件:php artisan make:migration create_table_liuyan --create=liuyan

先不忙这理解这个命令的意思,且看执行这个命令后生成的一个文件:

打开这个文件,是 php文件 :
<?php use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; class CreateTableLiuyan extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('liuyan', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('liuyan');
}
}
ok,对比一下,再来看前面的命令,解释一下:php.exe解释器通过artisan这个工具创建migration(英译迁移)文件,其中类名CreateTableLiuyan对应create_table_liuyan(随便取的,你 开心就好,不过还是按照规则来),最后--create=liuyan是固定格式就是创建一个liuyan的表
我们要来修改这个迁移的文件
<?php use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; class CreateTableLiuyan extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('liuyan', function (Blueprint $table) {
$table->increments('id');
$table->char('username',10);
$table->char('password',50);
$table->tinyInteger('sex');
$table->char('title',20);
$table->string('content',200);
$table->tinyInteger('pubtime');
$table->tinyInteger('ip');
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('liuyan');
}
}
ok,在cmd.exe窗口执行命令:php artisan migrate

ok,创建迁移表成功,用另一个cmd窗口查看一下wmsgs下是否有这张表:

ok,有了liuyan这个表了,另外三个表是migration自动生成的表
同样,如果我们想增加其中一个表列该怎么办,比如我们想增加一个表列email,输入如下命令:
php artisan make:migration add_email_to_liuyan --table=liuyan

生成一个表列迁移文件,同样也是php文件:

打开这个文件:
<?php use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; class AddEmailToLiuyan extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('liuyan', function (Blueprint $table) {
//
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('liuyan', function (Blueprint $table) {
//
});
}
}
将这个生成的php迁移文件改为如下:
<?php use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; class AddEmailToLiuyan extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('liuyan', function (Blueprint $table) {
$table->string('email');
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('liuyan', function (Blueprint $table) {
$table->dropCloumn('email');
});
}
}
在cmd.exe窗口执行命令:

ok,增加表列成功,开另一个cmd窗口看一下:

email成功添加到liuyan表中!
恭喜你,到这里数据库迁移入门 了

当然有很多命令还需要去多敲多记,加油!
程序员装逼只用命令行 ,不用命令行out了,哈哈
laravel数据库迁移(三)的更多相关文章
- Laravel数据库迁移
Laravel的数据迁移功能很好用,并且可以带来一系列好处.通过几条简单的 artisan 命令,就可以顺利上手,没有复杂的地方 注意:该系列命令对数据库非常危险,请准备一个单独的数据库作为配套练习, ...
- laravel数据库——迁移
1.简介 迁移就像数据库的版本控制,允许团队简单轻松的编辑并共享应用的数据库表结构,迁移通常和Laravel的结构构建器结对从而可以很容易地构建应用的数据库表结构. Laravel的Schema门面提 ...
- laravel 数据库迁移转 sql 语句
可以使用下面的命令 php artisan migrate --pretend --no-ansi 当然,你需要有可以 migrate 的东西. 数据库迁移导出到文件(使用命令) <?php n ...
- laravel 数据库迁移
问题:之前有创建迁移文件 并且执行过 如果删除迁移文件 再重新创建迁移文件时就有问题 提示找不到之前的迁移文件 /** 一开始执行的命令 php artisan make:migration crea ...
- laravel数据库迁移的migrate小解
当通过命令行:php artisan migrate:make create_authors_table --table=authors --create时,在 migration.php 中若Sch ...
- laravel数据库迁移 和 路由防攻击
命令:php artisan migrate 防攻击:
- 转: Laravel的数据库迁移 介绍的比较清晰
原文: https://blog.sbot.io/articles/12/Laravel-数据库迁移(Database-Migrations)操作实例 很多人可能在学习Laravel框架的时候,对La ...
- Laravel学习笔记(三)数据库 数据库迁移
该章节内容翻译自<Database Migration using Laravel>,一切版权为原作者. 原作者:Stable Host, LLC 翻译作者:Bowen Huang 正文: ...
- Laravel 5 基础(六)- 数据库迁移(Migrations)
database migrations 是laravel最强大的功能之一.数据库迁移可以理解为数据库的版本控制器. 在 database/migrations 目录中包含两个迁移文件,一个建立用户表, ...
随机推荐
- windows系统下fis3安装教程
注意:在安装fis3前必须安装node和npm,详情请见官网http://nodejs.org node版本要求 0.8.x,0.10.x, 0.12.x,4.x,6.x,不在此列表中的版本不予支持. ...
- 跨应用使用Spoon框架截图的方法
spoon框架是一个很棒的用例驱动跟测试结果生成加工的框架.但在使用spoon-client时,传入参数需要被测应用的activity实例,跨应用测试会很受限(当然也可能是因为我对android不熟导 ...
- [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] Reverse Linked List II 倒置链表之二
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- python网络编程-TCP协议中的三次握手和四次挥手(图解)
建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的. 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资 ...
- 用vue.js学习es6(六):Iterator和for...of循环
一.Iterator (遍历器)的概念: 遍历器(Iterator)就是这样一种机制.它是一种接口,为各种不同的数据结构提供统一的访问机制.任何数据结构只 要部署Iterator接口,就可以完成遍历操 ...
- chrome 更新flash插件
下载下面的插件并安装 https://fpdownload.macromedia.com/pub/labs/flashruntimes/flashplayer/install_flash_player ...
- EF 二级缓存 EFSecondLevelCache
EFSecondLevelCache ======= Entity Framework .x Second Level Caching Library. 二级缓存是一个查询缓存.EF命令的结果将存储在 ...