Laravel 5框架學(xué)習(xí)之?dāng)?shù)據(jù)庫遷移(Migrations)
database migrations 是laravel最強(qiáng)大的功能之一。數(shù)據(jù)庫遷移可以理解為數(shù)據(jù)庫的版本控制器。
在 database/migrations 目錄中包含兩個遷移文件,一個建立用戶表,一個用于用戶密碼重置。
在遷移文件中,up 方法用于創(chuàng)建數(shù)據(jù)表,down方法用于回滾,也就是刪除數(shù)據(jù)表。
執(zhí)行數(shù)據(jù)庫遷移
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ù)庫,可以看到產(chǎn)生了三張表。 migratoins 表是遷移記錄表,users 和 pasword_resets。
如果設(shè)計(jì)有問題,執(zhí)行數(shù)據(jù)庫回滾
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ù)庫,就剩下 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'); } }
自動添加了 id列,自動增長,timestamps() 會自動產(chǎn)生 created_at 和 updated_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(); }); }
執(zhí)行遷移:
php artisan migrate
現(xiàn)在有了新的數(shù)據(jù)表了。
假設(shè)我們需要添加一個新的字段,你可以回滾,然后修改遷移文件,再次執(zhí)行遷移,或者可以直接新建一個遷移文件
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ǔ)代碼。刪除這個文件,重新生成遷移文件,注意添加參數(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ù)庫。
如果我們?yōu)榱撕猛妫瑘?zhí)行回滾
php artisan migrate:rollback
excerpt 列沒有了。
以上所述就是本文的全部內(nèi)容了,希望能夠給大家熟練掌握Laravel5框架有所幫助。
- Laravel框架源碼解析之模型Model原理與用法解析
- Laravel框架源碼解析之入口文件原理分析
- Laravel框架源碼解析之反射的使用詳解
- Laravel 框架控制器 Controller原理與用法實(shí)例分析
- Laravel框架數(shù)據(jù)庫CURD操作、連貫操作總結(jié)
- PHP開發(fā)框架Laravel數(shù)據(jù)庫操作方法總結(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解決json_encode中文UNICODE轉(zhuǎn)碼問題
這篇文章主要介紹了基于php解決json_encode中文UNICODE轉(zhuǎn)碼問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11解決Laravel使用驗(yàn)證時跳轉(zhuǎn)到首頁的問題
當(dāng)我們在使用validate等方法進(jìn)行驗(yàn)證時,如果是錯誤,則會返回首頁,那么這個跳轉(zhuǎn)到首頁如何實(shí)現(xiàn)的呢?今天小編給大家分享一篇教程關(guān)于Laravel使用驗(yàn)證時跳轉(zhuǎn)到首頁的解決辦法,一起看看吧2021-10-10PHP Oauth授權(quán)和本地加密實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狿HP Oauth授權(quán)和本地加密實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08從零開始學(xué)YII2框架(三)擴(kuò)展插件yii2-gird
yii2-gird 插件是Yii2.0的一個擴(kuò)展。它在官方的girdview基礎(chǔ)上擴(kuò)展了一些實(shí)用的功能。非常好用,推薦使用哦。2014-08-08