惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
The Exploit Database - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
Simon Willison's Weblog
Simon Willison's Weblog
T
Tor Project blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
D
DataBreaches.Net
The Hacker News
The Hacker News
有赞技术团队
有赞技术团队
Latest news
Latest news
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
G
GRAHAM CLULEY
G
Google Developers Blog
W
WeLiveSecurity
Project Zero
Project Zero
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
F
Full Disclosure
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
F
Fortinet All Blogs
PCI Perspectives
PCI Perspectives
小众软件
小众软件
N
News | PayPal Newsroom
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
AI
AI
T
Tenable Blog
S
Schneier on Security
O
OpenAI News
The Register - Security
The Register - Security
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
T
Threatpost
Hacker News: Ask HN
Hacker News: Ask HN

博客园 - Zhangjinglin

Laravel 5 基础(十二)- 认证 Laravel 5 基础(十一)- Eloquent 关系 Laravel 5 基础(十一)- 子视图和表单复用 Laravel 5 基础(十一)- 表单验证 Laravel 5 基础(十)- 日期,Mutator 和 Scope Laravel 5 基础(九)- 表单 Laravel 5 基础(八)- 模型、控制器、视图基础流程 Laravel 5 基础(七)- Eloquent (laravel 的ORM) Laravel 5 基础(五)- 环境与配置 Laravel 5 基础(四)- Blade 简介 Laravel 5 基础(三)- 向视图传送数据(续) Laravel 5 基础(三)- 向视图传送数据 Laravel 5 基础(二)- 路由、控制器和视图简介 Laravel 5 基础(一)- Laravel入门和新建项目 Android 随机铃声管理器 ubuntu netbeans 字体解决 Mac OS 10.6 原版安装 git 强制恢复到某一版本 混乱中生存
Laravel 5 基础(六)- 数据库迁移(Migrations)
Zhangjinglin · 2015-04-01 · via 博客园 - Zhangjinglin

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 列没有了。