本文介紹了Laravel 的數據庫遷移的方法,分享給大家,具體如下:
生成遷移
--table 和 --create 選項可用來指定數據表的名稱,或是該遷移被執行時會創建的新數據表。這些選項需在預生成遷移文件時填入指定的數據表:
php artisan make:migration create_users_table php artisan make:migration create_users_table --create=users php artisan make:migration add_votes_to_users_table --table=users
添加字段
\database\migrations\2017_07_30_133748_create_users_table.php
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * 運行數據庫遷移 * * @return void */ public function up() { // Schema::create('users',function (Blueprint $table){ $table->increments('id')->comment('遞增ID'); $table->string('email',60)->comment('會員Email'); $table->string('phone',20)->comment('會員手機號'); $table->string('username',60)->comment('用戶名'); $table->string('password',32)->comment('用戶密碼'); $table->char('rank',10)->comment('會員等級'); $table->unsignedSmallInteger('sex')->comment('性別;0保密;1男;2女'); $table->unsignedSmallInteger('status')->comment('用戶狀態'); $table->ipAddress('last_ip')->default('0.0.0.0')->comment('最后一次登錄IP'); $table->timeTz('last_login')->comment('最后一次登錄時間'); $table->timestamps(); }); } /** * 回滾數據庫遷移 * * @return void */ public function down() { // Schema::drop('users'); } }
要創建一張新的數據表,可以使用 Schema facade 的 create 方法。create 方法接收兩個參數:第一個參數為數據表的名稱,第二個參數為一個 閉包 ,此閉包會接收一個用于定義新數據表的 Blueprint 對象
你可以方便地使用 hasTable 和 hasColumn 方法來檢查數據表或字段是否存在:
if (Schema::hasTable('users')) { // } if (Schema::hasColumn('users', 'email')) { // }
如果你想要在一個非默認的數據庫連接中進行數據庫結構操作,可以使用 connection 方法:
Schema::connection('foo')->create('users', function (Blueprint $table) { $table->increments('id'); });
你可以在數據庫結構構造器上設置 engine 屬性來設置數據表的存儲引擎:
Schema::create('users', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->increments('id'); });
重命名與刪除數據表
Schema::rename($from, $to);//重命名 //刪除已存在的數據表 Schema::drop('users'); Schema::dropIfExists('users');
創建字段
Schema::table('users', function (Blueprint $table) { $table->string('email'); });
命令 | 描述 |
---|---|
$table->bigIncrements('id'); | 遞增 ID(主鍵),相當于「UNSIGNED BIG INTEGER」型態。 |
$table->bigInteger('votes'); | 相當于 BIGINT 型態。 |
$table->binary('data'); | 相當于 BLOB 型態。 |
$table->boolean('confirmed'); | 相當于 BOOLEAN 型態。 |
$table->char('name', 4); | 相當于 CHAR 型態,并帶有長度。 |
$table->date('created_at'); | 相當于 DATE 型態 |
$table->dateTime('created_at'); | 相當于 DATETIME 型態。 |
$table->dateTimeTz('created_at'); | DATETIME (帶時區) 形態 |
$table->decimal('amount', 5, 2); | 相當于 DECIMAL 型態,并帶有精度與基數。 |
$table->double('column', 15, 8); | 相當于 DOUBLE 型態,總共有 15 位數,在小數點后面有 8 位數。 |
$table->enum('choices', ['foo', 'bar']); | 相當于 ENUM 型態。 |
$table->float('amount', 8, 2); | 相當于 FLOAT 型態,總共有 8 位數,在小數點后面有 2 位數。 |
$table->increments('id'); | 遞增的 ID (主鍵),使用相當于「UNSIGNED INTEGER」的型態。 |
$table->integer('votes'); | 相當于 INTEGER 型態。 |
$table->ipAddress('visitor'); | 相當于 IP 地址形態。 |
$table->json('options'); | 相當于 JSON 型態。 |
$table->jsonb('options'); | 相當于 JSONB 型態。 |
$table->longText('description'); | 相當于 LONGTEXT 型態。 |
$table->macAddress('device'); | 相當于 MAC 地址形態。 |
$table->mediumIncrements('id'); | 遞增 ID (主鍵) ,相當于「UNSIGNED MEDIUM INTEGER」型態。 |
$table->mediumInteger('numbers'); | 相當于 MEDIUMINT 型態。 |
$table->mediumText('description'); | 相當于 MEDIUMTEXT 型態。 |
$table->morphs('taggable'); | 加入整數 taggable_id 與字符串 taggable_type。 |
$table->nullableMorphs('taggable'); | 與 morphs() 字段相同,但允許為NULL。 |
$table->nullableTimestamps(); | 與 timestamps() 相同,但允許為 NULL。 |
$table->rememberToken(); | 加入 remember_token 并使用 VARCHAR(100) NULL。 |
$table->smallIncrements('id'); | 遞增 ID (主鍵) ,相當于「UNSIGNED SMALL INTEGER」型態。 |
$table->smallInteger('votes'); | 相當于 SMALLINT 型態。 |
$table->softDeletes(); | 加入 deleted_at 字段用于軟刪除操作。 |
$table->string('email'); | 相當于 VARCHAR 型態。 |
$table->string('name', 100); | 相當于 VARCHAR 型態,并帶有長度。 |
$table->text('description'); | 相當于 TEXT 型態。 |
$table->time('sunrise'); | 相當于 TIME 型態。 |
$table->timeTz('sunrise'); | 相當于 TIME (帶時區) 形態。 |
$table->tinyInteger('numbers'); | 相當于 TINYINT 型態。 |
$table->timestamp('added_on'); | 相當于 TIMESTAMP 型態。 |
$table->timestampTz('added_on'); | 相當于 TIMESTAMP (帶時區) 形態。 |
$table->timestamps(); | 加入 created_at 和 updated_at 字段。 |
$table->timestampsTz(); | 加入 created_at and updated_at (帶時區) 字段,并允許為NULL。 |
$table->unsignedBigInteger('votes'); | 相當于 Unsigned BIGINT 型態。 |
$table->unsignedInteger('votes'); | 相當于 Unsigned INT 型態。 |
$table->unsignedMediumInteger('votes'); | 相當于 Unsigned MEDIUMINT 型態。 |
$table->unsignedSmallInteger('votes'); | 相當于 Unsigned SMALLINT 型態。 |
$table->unsignedTinyInteger('votes'); | 相當于 Unsigned TINYINT 型態。 |
$table->uuid('id'); | 相當于 UUID 型態。 |
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。