Laravel 5框架學(xué)習(xí)之?dāng)?shù)據(jù)庫(kù)遷移(Migrations)
database migrations 是laravel最強(qiáng)大的功能之一。數(shù)據(jù)庫(kù)遷移可以理解為數(shù)據(jù)庫(kù)的版本控制器。
在 database/migrations 目錄中包含兩個(gè)遷移文件,一個(gè)建立用戶表,一個(gè)用于用戶密碼重置。
在遷移文件中,up 方法用于創(chuàng)建數(shù)據(jù)表,down方法用于回滾,也就是刪除數(shù)據(jù)表。
執(zhí)行數(shù)據(jù)庫(kù)遷移
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數(shù)據(jù)庫(kù),可以看到產(chǎn)生了三張表。 migratoins 表是遷移記錄表,users 和 pasword_resets。
如果設(shè)計(jì)有問題,執(zhí)行數(shù)據(jù)庫(kù)回滾
php artisan migrate:rollback
#輸出
Rolled back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_000000_create_users_table
再次查看mysql數(shù)據(jù)庫(kù),就剩下 migrations 表了, users password_resets 被刪除了。
修改遷移文件,再次執(zhí)行遷移。
新建遷移
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');
}
}
自動(dòng)添加了 id列,自動(dòng)增長(zhǎng),timestamps() 會(huì)自動(dòng)產(chǎn)生 created_at 和 updated_at 兩個(gè)時(shí)間列。我們添加一些字段:
public function up()
{
Schema::create('articles', function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamp('published_at');
$table->timestamps();
});
}
執(zhí)行遷移:
php artisan migrate
現(xiàn)在有了新的數(shù)據(jù)表了。
假設(shè)我們需要添加一個(gè)新的字段,你可以回滾,然后修改遷移文件,再次執(zhí)行遷移,或者可以直接新建一個(gè)遷移文件
php artisan make:migration add_excerpt_to_articels_table
查看新產(chǎn)生的遷移文件
<?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為我們生成基礎(chǔ)代碼。刪除這個(gè)文件,重新生成遷移文件,注意添加參數(shù):
php artisan make:migration add_excerpt_to_articels_table --table='articles'
現(xiàn)在,up 方法里面有了初始代碼。
public function up()
{
Schema::table('articles', function(Blueprint $table)
{
//
});
}
添加實(shí)際的數(shù)據(jù)修改代碼:
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() 表示字段也可以為空。
再次執(zhí)行遷移并檢查數(shù)據(jù)庫(kù)。
如果我們?yōu)榱撕猛妫瑘?zhí)行回滾
php artisan migrate:rollback
excerpt 列沒有了。
以上所述就是本文的全部?jī)?nèi)容了,希望能夠給大家熟練掌握Laravel5框架有所幫助。
- Laravel框架源碼解析之模型Model原理與用法解析
- Laravel框架源碼解析之入口文件原理分析
- Laravel框架源碼解析之反射的使用詳解
- Laravel 框架控制器 Controller原理與用法實(shí)例分析
- Laravel框架數(shù)據(jù)庫(kù)CURD操作、連貫操作總結(jié)
- PHP開發(fā)框架Laravel數(shù)據(jù)庫(kù)操作方法總結(jié)
- Laravel框架中擴(kuò)展函數(shù)、擴(kuò)展自定義類的方法
- Laravel框架路由配置總結(jié)、設(shè)置技巧大全
- Laravel 5 框架入門(一)
- Laravel 5框架學(xué)習(xí)之向視圖傳送數(shù)據(jù)
- Laravel 5框架學(xué)習(xí)之用戶認(rèn)證
- Laravel框架集合用法實(shí)例淺析
相關(guān)文章
PHP定時(shí)執(zhí)行任務(wù)實(shí)現(xiàn)方法詳解(Timer)
這篇文章主要介紹了PHP定時(shí)執(zhí)行任務(wù)實(shí)現(xiàn)方法詳解,定時(shí)任務(wù)在web應(yīng)用程序中比較常見,實(shí)現(xiàn)定時(shí)任務(wù)主要有兩種方案:1)使用Crontab命令,2)配合使用ignore_user_abort()和set_time_limit(),有需要的朋友可以來(lái)借鑒下。2015-07-07
PHP命名空間(namespace)的使用基礎(chǔ)及示例
本文介紹了PHP命名空間的一些術(shù)語(yǔ),其解析規(guī)則,以及一些高級(jí)功能的應(yīng)用,希望能夠幫助讀者在項(xiàng)目中真正使用命名空間。2014-08-08
thinkPHP統(tǒng)計(jì)排行與分頁(yè)顯示功能示例
這篇文章主要介紹了thinkPHP統(tǒng)計(jì)排行與分頁(yè)顯示功能,結(jié)合實(shí)例形式分析了thinkPHP數(shù)據(jù)庫(kù)查詢與結(jié)果分頁(yè)顯示相關(guān)操作技巧,需要的朋友可以參考下2016-12-12
php實(shí)現(xiàn)微信支付之現(xiàn)金紅包
這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)微信支付之現(xiàn)金紅包,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
ThinkPHP查詢返回簡(jiǎn)單字段數(shù)組的方法
這篇文章主要介紹了ThinkPHP查詢返回簡(jiǎn)單字段數(shù)組的方法,是ThinkPHP查詢功能中一個(gè)非常實(shí)用的技巧,需要的朋友可以參考下2014-08-08
PHP 自動(dòng)加載的簡(jiǎn)單實(shí)現(xiàn)(推薦)
下面小編就為大家?guī)?lái)一篇PHP 自動(dòng)加載的簡(jiǎn)單實(shí)現(xiàn)(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08

