Laravel 5 migrate

Vckin發表於2018-12-12

建立表

php artisan make:migration create_user_table --create="users"

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

增加欄位

php artisan make:migration add_users_table --table="users"

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddIsAdminToUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            //
            $table->boolean('is_admin')->default(false);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            //
            $table->dropColumn('is_admin');
        });
    }
}

命令

php artisan migrate 執行新增加資料
php artisan migrate:rollback 回滾到上一次的表結構遷移
php artisan migrate:reset 回滾所有遷移
php artisan migrate:refresh [--seed] 回滾所有遷移並重新執行資料遷移(seed 資料部分的載入)

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

相關文章