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数据库迁移(三)的更多相关文章

  1. Laravel数据库迁移

    Laravel的数据迁移功能很好用,并且可以带来一系列好处.通过几条简单的 artisan 命令,就可以顺利上手,没有复杂的地方 注意:该系列命令对数据库非常危险,请准备一个单独的数据库作为配套练习, ...

  2. laravel数据库——迁移

    1.简介 迁移就像数据库的版本控制,允许团队简单轻松的编辑并共享应用的数据库表结构,迁移通常和Laravel的结构构建器结对从而可以很容易地构建应用的数据库表结构. Laravel的Schema门面提 ...

  3. laravel 数据库迁移转 sql 语句

    可以使用下面的命令 php artisan migrate --pretend --no-ansi 当然,你需要有可以 migrate 的东西. 数据库迁移导出到文件(使用命令) <?php n ...

  4. laravel 数据库迁移

    问题:之前有创建迁移文件 并且执行过 如果删除迁移文件 再重新创建迁移文件时就有问题 提示找不到之前的迁移文件 /** 一开始执行的命令 php artisan make:migration crea ...

  5. laravel数据库迁移的migrate小解

    当通过命令行:php artisan migrate:make create_authors_table --table=authors --create时,在 migration.php 中若Sch ...

  6. laravel数据库迁移 和 路由防攻击

    命令:php  artisan  migrate 防攻击:

  7. 转: Laravel的数据库迁移 介绍的比较清晰

    原文: https://blog.sbot.io/articles/12/Laravel-数据库迁移(Database-Migrations)操作实例 很多人可能在学习Laravel框架的时候,对La ...

  8. Laravel学习笔记(三)数据库 数据库迁移

    该章节内容翻译自<Database Migration using Laravel>,一切版权为原作者. 原作者:Stable Host, LLC 翻译作者:Bowen Huang 正文: ...

  9. Laravel 5 基础(六)- 数据库迁移(Migrations)

    database migrations 是laravel最强大的功能之一.数据库迁移可以理解为数据库的版本控制器. 在 database/migrations 目录中包含两个迁移文件,一个建立用户表, ...

随机推荐

  1. 使用JavaMail发送邮件

    一.邮件的相关概念 邮件协议.主要包括: SMTP协议:Simple Mail Transfer Protocol,即简单邮件传输协议,用于发送电子邮件 POP3协议:Post Office Prot ...

  2. Basic EEG waves 四种常见EEG波形

    Source: https://www.medicine.mcgill.ca/physio/vlab/biomed_signals/eeg_n.htm The electroencephalogram ...

  3. [LeetCode] Binary Watch 二进制表

    A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...

  4. [LeetCode] Paint House II 粉刷房子之二

    There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...

  5. web兼容学习分析笔记-margin 和padding浏览器解析差异

    二.margin 和padding浏览器解析差异 只有默认margin的元素 <body>margin:8px  margin:15px 10px 15px 10px(IE7) <b ...

  6. 基于ionic+angulajs的混合开发实现地铁APP

    基于ionic+angulajs的混合开发实现地铁APP 注:本博文为博主原创,转载时请注明出处. 项目源码地址:https://github.com/zhangxy1035/SubwayMap 一. ...

  7. jquery $.extend()扩展插件获取焦点或失去焦点事件

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. JavaScript中---作用域

    作用域: 变量还有函数作用的范围. 浏览器的内核主要有两大功能,一个是渲染页面,另一个就是我们的JavaScript的解释器了. 我们主要来说说JavaScript解释器,在解析时是怎么样的工作原理. ...

  9. 在页面的el表达式是如何判断null的

    <c:if test="${not empty message}"> <div id="message" class="alert ...

  10. asp.net配置web.config支持jQuery.Uploadify插件上传大文件

    配置web.config有两处地方需要配置,分别是集成模式和经典模式. 集成模式: <!--文件上传大小设置--> <httpRuntime requestValidationMod ...