Laravel 5資料庫遷移的學習

不言
發布: 2023-04-01 06:56:01
原創
1267 人瀏覽過

本文要介紹給大家的是Laravel5框架中最強大的功能之一資料庫遷移(database migrations),本文詳細介紹給大家介紹資料庫遷移的步驟和方法,非常實用,有需要的小夥伴可以參考下。

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(&#39;articles&#39;, function(Blueprint $table)
 {
  $table->increments(&#39;id&#39;);
  $table->timestamps();
 });
 }

 /**
 * Reverse the migrations.
 *
 * @return void
 */
 public function down()
 {
 Schema::drop(&#39;articles&#39;);
 }

}
登入後複製

自動新增了 id列,自動成長,timestamps() 會自動產生 created_at 和 updated_at 兩個時間列。我們新增一些欄位:

 public function up()
 {
 Schema::create(&#39;articles&#39;, function(Blueprint $table)
 {
  $table->increments(&#39;id&#39;);
      $table->string(&#39;title&#39;);
      $table->text(&#39;body&#39;);
      $table->timestamp(&#39;published_at&#39;);
  $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=&#39;articles&#39;
登入後複製

現在,up 方法裡面有了初始程式碼。

 public function up()
 {
 Schema::table(&#39;articles&#39;, function(Blueprint $table)
 {
  //
 });
 }
登入後複製

新增實際的資料修改程式碼:

 public function up()
 {
 Schema::table(&#39;articles&#39;, function(Blueprint $table)
 {
  $table->text(&#39;excerpt&#39;)->nullable();
 });
 }
 
 public function down()
 {
 Schema::table(&#39;articles&#39;, function(Blueprint $table)
 {
  $table->dropColumn(&#39;excerpt&#39;);
 });
 }
登入後複製

nullable() 表示字段也可以為空。

再次執行遷移並檢查資料庫。

如果我們為了好玩,執行回滾

php artisan migrate:rollback
登入後複製

excerpt 欄位就沒有了。

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關建議:

Laravel5框架的子視圖和表單重複使用的解析

Laravel 4 的Pages和表單驗證

以上是Laravel 5資料庫遷移的學習的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!