Laravel 5架構學習之資料庫遷移(Migrations)_php執行個體

來源:互聯網
上載者:User
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 下產生了新的檔案。

<?phpuse 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

查看新產生的遷移檔案

<?phpuse 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架構有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.