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. 产生冠军 hdoj_2094 巧用set

    产生冠军 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. C#体检套餐项目

    使用泛型集合写的一个小项目 1.要实现新建体检套餐,并且如果已经有了该体检套餐就不能再次新建, 2.要实现套餐列表动态更新,没添加一个体检套餐,在套餐列表里就自动添加一项; 3.向当前套餐类表里添加检 ...

  3. [LeetCode] Island Perimeter 岛屿周长

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  4. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  5. [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  6. 【转】Windows平台下的Subversion安装配置新手指南

    原文地址:http://developer.51cto.com/art/201005/199628.htm 本文介绍Subversion安装配置快速指南,首先讲Subversion的安装和配置,Uni ...

  7. 【BZOJ 3993】【SDOI 2015】星际战争

    http://www.lydsy.com/JudgeOnline/problem.php?id=3993 调了好长时间啊 这道题设时间为time,那么对于m个武器从S向这m个点连容量为time*Bi的 ...

  8. Block常用方法以及注意事项

    1. ViewController间传递数据 2. Block的@property必须定义为copy

  9. echarts-在现实标题中显示百分比

    如图:需要在标题显示所占百分比 使用方式:途中标记部分 series : [{ name: '类型', type: 'pie', radius : '55%', center: ['50%', '60 ...

  10. 关于PHP扩展开发(收藏)

    一.Linux shell命令: ls –lh    查看文件大小 du –a    查看文件及文件夹大小 -------------------------- nginx ------------- ...