日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Laravel 的数据库迁移

發布時間:2025/3/18 数据库 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Laravel 的数据库迁移 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

生成遷移

--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

<?phpuse 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 型態。

字段修飾

Schema::table('users', function (Blueprint $table) {$table->string('email')->nullable(); }); ModifierDescription
->after('column')將此字段放置在其它字段「之后」(僅限 MySQL)
->comment('my comment')增加注釋
->default($value)為此字段指定「默認」值
->first()將此字段放置在數據表的「首位」(僅限 MySQL)
->nullable()此字段允許寫入 NULL 值
->storedAs($expression)創建一個存儲的生成字段 (僅限 MySQL)
->unsigned()設置?integer?字段為?UNSIGNED
->virtualAs($expression)創建一個虛擬的生成字段 (僅限 MySQL)

字段更新

Schema::table('users', function (Blueprint $table) {$table->string('phone',20)->change();$table->string('username',60)->->nullable()->change(); });

重命名字段

Schema::table('users', function (Blueprint $table) {$table->renameColumn('from', 'to'); });

字段移除

Schema::table('users', function (Blueprint $table) {$table->dropColumn(['last_ip', 'last_login']); });

? ? ?在使用字段更新,重命名字段,字段移除之前,請務必在你的 composer.json文件require鍵名中添加<?"doctrine/dbal": "^2.5">值。然后composer update進行更新或

composer require doctrine/dbal

創建索引

$table->string('email')->unique(); CommandDescription
$table->primary('id');加入主鍵。
$table->primary(['first', 'last']);加入復合鍵。
$table->unique('email');加入唯一索引。
$table->unique('state', 'my_index_name');自定義索引名稱。
$table->unique(['first', 'last']);加入復合唯一鍵。
$table->index('state');加入基本索引。

開啟和關閉外鍵約束

Schema::enableForeignKeyConstraints();Schema::disableForeignKeyConstraints();

運行遷移

php artisan migrate

在線上環境強制執行遷移

php artisan migrate --force

回滾遷移

若要回滾最后一次遷移,則可以使用 rollback 命令。此命令是對上一次執行的「批量」遷移回滾,其中可能包括多個遷移文件:

php artisan migrate:rollback

在 rollback 命令后加上 step 參數,你可以限制回滾遷移的個數。例如,下面的命令將會回滾最后的 5 個遷移。

php artisan migrate:rollback --step=5

migrate:reset 命令可以回滾應用程序中的所有遷移:

php artisan migrate:reset

使用單個命令來執行回滾或遷移

migrate:refresh 命令不僅會回滾數據庫的所有遷移還會接著運行 migrate 命令。所以此命令可以有效的重新創建整個數據庫:

php artisan migrate:refresh // 刷新數據庫結構并執行數據填充 php artisan migrate:refresh --seed

使用 refresh 命令并加上 step 參數,你也可以限制執行回滾和再遷移的個數。比如,下面的命令會回滾并再遷移最后的 5 個遷移:

php artisan migrate:refresh --step=5

無法生成遷移文件

在 Laravel 項目中,由于測試,有時候用 PHP artisan make:migration create_xxx_table 創建數據庫遷移。如果把創建的遷移文件 database/migrations/2017_07_30_133748_create_xxx_table.php 文件給刪除了,再次執行 php artisan make:migration create_xxx_table 會報錯:

[ErrorException] include(E:\laraver\vendor\composer/../../database/migrations/2017_07_30_133748_create_users_table.php): failed to open stream: No such file or directory

重新運行?composer update?又可以執行上面的命令了。

?

全棧工程師技術QQ交流群:368683534

轉載于:https://my.oschina.net/zhiqiangwang/blog/1492411

總結

以上是生活随笔為你收集整理的Laravel 的数据库迁移的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。