Laravel migration (資料庫遷移) 的使用

TF-Frank發表於2020-05-28

簡介:遷移就像是資料庫的版本控制,允許團隊簡單輕鬆的編輯並共享應用的資料庫表結構,遷移通常和 Laravel 的 資料庫結構生成器配合使用,讓你輕鬆地構建資料庫結構。如果你曾經試過讓同事手動在資料庫結構中新增欄位,那麼資料庫遷移可以讓你不再需要做這樣的事情。

來自laravel社群:資料庫遷移《Laravel 5.8 中文文件》
具體使用:
1、生成遷移:
使用 Artisan 命令生成遷移檔案:

php artisan make:migration create_members_table

遷移檔案在 database/migrations下。每個遷移檔名都包含時間戳,以便讓 Laravel 確認遷移的順序。

2、遷移結構
具體欄位參考laravle社群:資料庫遷移《Laravel 5.8 中文文件》

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMembersTable extends Migration
{
    /**
     * Run the migrations.
     * @return void
     */
    public function up()
    {
        Schema::create('members', function (Blueprint $table)
        {
            $table->bigIncrements('id');
            $table->string('u_openid',100)->comment("openid");
            $table->string('u_name',50)->comment('使用者暱稱');
            $table->string('u_face',255)->comment('使用者頭像');
            $table->bigInteger('u_integral')->comment('積分');
            $table->string('u_random',30)->comment('使用者隨機碼');
            $table->decimal('u_remainder',10,2)->comment('餘額');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('members');
    }
}

3、執行遷移

php artisan migrate
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章