程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> Laravel 5框架學習之數據庫遷移(Migrations),laravelmigrations

Laravel 5框架學習之數據庫遷移(Migrations),laravelmigrations

編輯:關於PHP編程

Laravel 5框架學習之數據庫遷移(Migrations),laravelmigrations


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 表是遷移記錄表,users 和 pasword_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_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();
 });
 }

執行遷移:

復制代碼 代碼如下:
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 列沒有了。

以上所述就是本文的全部內容了,希望能夠給大家熟練掌握Laravel5框架有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved