database migrations 是laravel最强大的功能之一。数据库迁移可以理解为数据库的版本控制器。

database/migrations 目录中包含两个迁移文件,一个建立用户表,一个用于用户密码重置。

在迁移文件中,up 方法用于创建数据表,down方法用于回滚,也就是删除数据表。

  • 执行数据库迁移
php artisan migrate
#输出
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table

查看mysql数据库,可以看到产生了三张表。 migratoins 表是迁移记录表,userspasword_resets

  • 如果设计有问题,执行数据库回滚
php artisan migrate:rollback
#输出
Rolled back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_000000_create_users_table

再次查看mysql数据库,就剩下 migrations 表了, users password_resets 被删除了。

修改迁移文件,再次执行迁移。

  • 新建迁移
php artisan make:migration create_article_table --create='articles'
#输出
Created Migration: 2015_03_28_050138_create_article_table

database/migrations 下生成了新的文件。

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; class CreateArticleTable 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::drop('articles');
} }

自动添加了 id列,自动增长,timestamps() 会自动产生 created_atupdated_at 两个时间列。我们添加一些字段:

	public function up()
{
Schema::create('articles', function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamp('published_at');
$table->timestamps();
});
}

执行迁移:

php artisan migrate

现在有了新的数据表了。

假设我们需要添加一个新的字段,你可以回滚,然后修改迁移文件,再次执行迁移,或者可以直接新建一个迁移文件

php artisan make:migration add_excerpt_to_articels_table

查看新产生的迁移文件

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; class AddExcerptToArticelsTable extends Migration { /**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
} }

只有空的 up 和 down 方法。我们可以手工添加代码,或者我们让laravel为我们生成基础代码。删除这个文件,重新生成迁移文件,注意添加参数:

php artisan make:migration add_excerpt_to_articels_table --table='articles'

现在,up 方法里面有了初始代码。

	public function up()
{
Schema::table('articles', function(Blueprint $table)
{
//
});
}

添加实际的数据修改代码:

	public function up()
{
Schema::table('articles', function(Blueprint $table)
{
$table->text('excerpt')->nullable();
});
} public function down()
{
Schema::table('articles', function(Blueprint $table)
{
$table->dropColumn('excerpt');
});
}

nullable() 表示字段也可以为空。

再次执行迁移并检查数据库。

如果我们为了好玩,执行回滚

php artisan migrate:rollback

excerpt 列没有了。

Laravel 5 基础(六)- 数据库迁移(Migrations)的更多相关文章

  1. Laravel 实践之路: 数据库迁移与数据填充

    数据库迁移实际上就是对数据库库表的结构变化做版本控制,之前对数据库库表结构做修改的方式比较原始,比如说对某张库表新增了一个字段,都是直接在库表中执行alter table xxx add .. 的方式 ...

  2. Laravel中如何做数据库迁移

    总的来说,做一次独立数据库迁移只需要三步,分别是创建迁移文件.修改迁移文件.运行迁移 1.创建数据库迁移文件php artisan make:migration create_articles_tab ...

  3. laravel基础课程---16、数据迁移(数据库迁移是什么)

    laravel基础课程---16.数据迁移(数据库迁移是什么) 一.总结 一句话总结: 是什么:数据库迁移就像是[数据库的版本控制],可以让你的团队轻松修改并共享应用程序的数据库结构. 使用场景:解决 ...

  4. laravel执行数据库迁移的过程中出现Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Operation timed out (SQL: select * from information_schema.tables where table_schema = shop and table_name = migrations

    向customers表添加字段phone php artisan make:migration add_phone_to_customers_table 问题: 解决方法: 将DB_HOST配置项修改 ...

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

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

  6. laravel数据库迁移(三)

    laravel号称世界上最好的框架,数据库迁移算上一个,在这里先简单入个门: laravel很强大,它把表中的操作写成了migrations迁移文件,然后可以直接通过迁移文件来操作表.所以 , 数据迁 ...

  7. Laravel数据库迁移

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

  8. Laravel 5.2数据库--迁移migration

    Laravel中的migrations文件存放的是数据库表文件等结构,可以说是一个跟git差不多的,可以说像是数据库的版本控制器,所以可以叫做迁移.因为它可以很快速的很容易地构建应用的数据库表结构. ...

  9. Laravel 5.2 数据库迁移和数据填充

    一.数据库迁移 Laravel 的数据库迁移提供了对数据库.表.字段.索引的一系列相关操作.下面以创建友情链接表为例. 1. 创建迁移 使用 Artisan 命令  php artisan make: ...

随机推荐

  1. 【原】基于64位Centos6.2的mcrouter使用简介

    此文转载必须注明原文地址,请尊重作者的劳动成果!  http://www.cnblogs.com/lyongerr/p/5040071.html 目录 文档控制... 2 1 mcrouter简介.. ...

  2. Configuring HugePages for Oracle on Linux (x86-64)

    Introduction Configuring HugePages Force Oracle to use HugePages (USE_LARGE_PAGES) Disabling Transpa ...

  3. Oracle Cluster Registry Location to be Added is not Accessible

    APPLIES TO: Oracle Server - Enterprise Edition - Version 11.2.0.1 and laterInformation in this docum ...

  4. 项目积累——SQL积累

    select sum(njts)-sum(ysyts) from njsyqk where ygdh='888882' and ((yxbz is null) or (yxbz='1')) selec ...

  5. jboss7访问日志功能及使用goaccess工具分析

    网络上虽然很多文章分别讲到jboss7的访问日志如何配置,goaccess工具怎么分析nginx/tomcat等日志.但将两者放在一起即“通过goaccess分析jboss访问日志”的倒是没搜索到. ...

  6. 【LeetCode】11. Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  7. 学习资料 数据查询语言DQL

    数据查询语言DQL介绍及其应用: 查询是SQL语言的核心,SQL语言只提供唯一一个用于数据库查询的语句,即SELECT语句.用于表达SQL查询的SELECT语句是功能最强也是最复杂的SQL语句,它提供 ...

  8. ASPxGridView中批量提交及个别提交的写法

    //获取chech box ID protected string GetProtoID() { string protoId = ""; //获取选中的记录Id List< ...

  9. velocity freemarker比较

    相比较 FreeMarker 而言,Velocity 更加简单.轻量级,但它的功能却没有 FreeMarker 那么强大. 对于大部分的应用来说,使用 FreeMarker 比 Velocity 更简 ...

  10. MacOSX和Windows 8的完美融合

    MacOSX和Windows8的完美融合 一般情况下我们要在MACOS系统下运行Windows软件怎么办呢?一种方法我们可以装CrossOver这款软件,然后在configuration->in ...