在資料庫遷移中
每次遷移都應該有 up() 方法和 down() 方法。up() 方法的目的是執行將資料庫模式置於新狀態所需的操作,down() 方法的目的是反轉 up() 方法執行的任何操作。確保 down() 方法正確地反轉你的操作對於能夠回滾資料庫模式更改至關重要。
示例遷移檔案可能如下所示:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddLastLoggedInToUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dateTime('last_logged_in')->nullable();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('last_logged_in');
        });
    }
}
執行此遷移時,Laravel 將生成以下 SQL 以針對你的資料庫執行:
ALTER TABLE `users` ADD `last_logged_in` DATETIME NULL