《PHP學(xué)習(xí):Laravel 的數(shù)據(jù)庫(kù)遷移的方法》要點(diǎn):
本文介紹了PHP學(xué)習(xí):Laravel 的數(shù)據(jù)庫(kù)遷移的方法,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP編程本文介紹了Laravel 的數(shù)據(jù)庫(kù)遷移的方法,分享給大家,具體如下:
PHP編程生成遷移
PHP編程--table 和 --create 選項(xiàng)可用來(lái)指定數(shù)據(jù)表的名稱,或是該遷移被執(zhí)行時(shí)會(huì)創(chuàng)建的新數(shù)據(jù)表.這些選項(xiàng)需在預(yù)生成遷移文件時(shí)填入指定的數(shù)據(jù)表:
PHP編程
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
PHP編程添加字段
PHP編程\database\migrations\2017_07_30_133748_create_users_table.php
PHP編程
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* 運(yùn)行數(shù)據(jù)庫(kù)遷移
*
* @return void
*/
public function up()
{
//
Schema::create('users',function (Blueprint $table){
$table->increments('id')->comment('遞增ID');
$table->string('email',60)->comment('會(huì)員Email');
$table->string('phone',20)->comment('會(huì)員手機(jī)號(hào)');
$table->string('username',60)->comment('用戶名');
$table->string('password',32)->comment('用戶密碼');
$table->char('rank',10)->comment('會(huì)員等級(jí)');
$table->unsignedSmallInteger('sex')->comment('性別;0保密;1男;2女');
$table->unsignedSmallInteger('status')->comment('用戶狀態(tài)');
$table->ipAddress('last_ip')->default('0.0.0.0')->comment('最后一次登錄IP');
$table->timeTz('last_login')->comment('最后一次登錄時(shí)間');
$table->timestamps();
});
}
/**
* 回滾數(shù)據(jù)庫(kù)遷移
*
* @return void
*/
public function down()
{
//
Schema::drop('users');
}
}
PHP編程要?jiǎng)?chuàng)建一張新的數(shù)據(jù)表,可以使用 Schema facade 的 create 方法.create 方法接收兩個(gè)參數(shù):第一個(gè)參數(shù)為數(shù)據(jù)表的名稱,第二個(gè)參數(shù)為一個(gè) 閉包 ,此閉包會(huì)接收一個(gè)用于定義新數(shù)據(jù)表的 Blueprint 對(duì)象
PHP編程你可以方便地使用 hasTable 和 hasColumn 方法來(lái)檢查數(shù)據(jù)表或字段是否存在:
PHP編程
if (Schema::hasTable('users')) {
//
}
if (Schema::hasColumn('users', 'email')) {
//
}
PHP編程如果你想要在一個(gè)非默認(rèn)的數(shù)據(jù)庫(kù)連接中進(jìn)行數(shù)據(jù)庫(kù)結(jié)構(gòu)操作,可以使用 connection 方法:
PHP編程
Schema::connection('foo')->create('users', function (Blueprint $table) {
$table->increments('id');
});
PHP編程你可以在數(shù)據(jù)庫(kù)結(jié)構(gòu)構(gòu)造器上設(shè)置 engine 屬性來(lái)設(shè)置數(shù)據(jù)表的存儲(chǔ)引擎:
PHP編程
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
});
PHP編程重命名與刪除數(shù)據(jù)表
PHP編程
Schema::rename($from, $to);//重命名
//刪除已存在的數(shù)據(jù)表
Schema::drop('users');
Schema::dropIfExists('users');
PHP編程創(chuàng)建字段
PHP編程
Schema::table('users', function (Blueprint $table) {
$table->string('email');
});
命令 | 描述 |
---|---|
$table->bigIncrements('id'); | 遞增 ID(主鍵),相當(dāng)于「UNSIGNED BIG INTEGER」型態(tài). |
$table->bigInteger('votes'); | 相當(dāng)于 BIGINT 型態(tài). |
$table->binary('data'); | 相當(dāng)于 BLOB 型態(tài). |
$table->boolean('confirmed'); | 相當(dāng)于 BOOLEAN 型態(tài). |
$table->char('name', 4); | 相當(dāng)于 CHAR 型態(tài),并帶有長(zhǎng)度. |
$table->date('created_at'); | 相當(dāng)于 DATE 型態(tài) |
$table->dateTime('created_at'); | 相當(dāng)于 DATETIME 型態(tài). |
$table->dateTimeTz('created_at'); | DATETIME (帶時(shí)區(qū)) 形態(tài) |
$table->decimal('amount', 5, 2); | 相當(dāng)于 DECIMAL 型態(tài),并帶有精度與基數(shù). |
$table->double('column', 15, 8); | 相當(dāng)于 DOUBLE 型態(tài),總共有 15 位數(shù),在小數(shù)點(diǎn)后面有 8 位數(shù). |
$table->enum('choices', ['foo', 'bar']); | 相當(dāng)于 ENUM 型態(tài). |
$table->float('amount', 8, 2); | 相當(dāng)于 FLOAT 型態(tài),總共有 8 位數(shù),在小數(shù)點(diǎn)后面有 2 位數(shù). |
$table->increments('id'); | 遞增的 ID (主鍵),使用相當(dāng)于「UNSIGNED INTEGER」的型態(tài). |
$table->integer('votes'); | 相當(dāng)于 INTEGER 型態(tài). |
$table->ipAddress('visitor'); | 相當(dāng)于 IP 地址形態(tài). |
$table->json('options'); | 相當(dāng)于 JSON 型態(tài). |
$table->jsonb('options'); | 相當(dāng)于 JSONB 型態(tài). |
$table->longText('description'); | 相當(dāng)于 LONGTEXT 型態(tài). |
$table->macAddress('device'); | 相當(dāng)于 MAC 地址形態(tài). |
$table->mediumIncrements('id'); | 遞增 ID (主鍵) ,相當(dāng)于「UNSIGNED MEDIUM INTEGER」型態(tài). |
$table->mediumInteger('numbers'); | 相當(dāng)于 MEDIUMINT 型態(tài). |
$table->mediumText('description'); | 相當(dāng)于 MEDIUMTEXT 型態(tài). |
$table->morphs('taggable'); | 加入整數(shù)?taggable_id?與字符串?taggable_type. |
$table->nullableMorphs('taggable'); | 與?morphs()?字段相同,但允許為NULL. |
$table->nullableTimestamps(); | 與?timestamps()?相同,但允許為 NULL. |
$table->rememberToken(); | 加入?remember_token?并使用 VARCHAR(100) NULL. |
$table->smallIncrements('id'); | 遞增 ID (主鍵) ,相當(dāng)于「UNSIGNED SMALL INTEGER」型態(tài). |
$table->smallInteger('votes'); | 相當(dāng)于 SMALLINT 型態(tài). |
$table->softDeletes(); | 加入?deleted_at?字段用于軟刪除操作. |
$table->string('email'); | 相當(dāng)于 VARCHAR 型態(tài). |
$table->string('name', 100); | 相當(dāng)于 VARCHAR 型態(tài),并帶有長(zhǎng)度. |
$table->text('description'); | 相當(dāng)于 TEXT 型態(tài). |
$table->time('sunrise'); | 相當(dāng)于 TIME 型態(tài). |
$table->timeTz('sunrise'); | 相當(dāng)于 TIME (帶時(shí)區(qū)) 形態(tài). |
$table->tinyInteger('numbers'); | 相當(dāng)于 TINYINT 型態(tài). |
$table->timestamp('added_on'); | 相當(dāng)于 TIMESTAMP 型態(tài). |
$table->timestampTz('added_on'); | 相當(dāng)于 TIMESTAMP (帶時(shí)區(qū)) 形態(tài). |
$table->timestamps(); | 加入?created_at?和?updated_at?字段. |
$table->timestampsTz(); | 加入?created_at?and?updated_at?(帶時(shí)區(qū)) 字段,并允許為NULL. |
$table->unsignedBigInteger('votes'); | 相當(dāng)于 Unsigned BIGINT 型態(tài). |
$table->unsignedInteger('votes'); | 相當(dāng)于 Unsigned INT 型態(tài). |
$table->unsignedMediumInteger('votes'); | 相當(dāng)于 Unsigned MEDIUMINT 型態(tài). |
$table->unsignedSmallInteger('votes'); | 相當(dāng)于 Unsigned SMALLINT 型態(tài). |
$table->unsignedTinyInteger('votes'); | 相當(dāng)于 Unsigned TINYINT 型態(tài). |
$table->uuid('id'); | 相當(dāng)于 UUID 型態(tài). |
PHP編程字段修飾
PHP編程
Schema::table('users', function (Blueprint $table) {
$table->string('email')->nullable();
});
Modifier | Description |
---|---|
->after('column') | 將此字段放置在其它字段「之后」(僅限 MySQL) |
->comment('my comment') | 增加注釋 |
->default($value) | 為此字段指定「默認(rèn)」值 |
->first() | 將此字段放置在數(shù)據(jù)表的「首位」(僅限 MySQL) |
->nullable() | 此字段允許寫入 NULL 值 |
->storedAs($expression) | 創(chuàng)建一個(gè)存儲(chǔ)的生成字段 (僅限 MySQL) |
->unsigned() | 設(shè)置?integer?字段為?UNSIGNED |
->virtualAs($expression) | 創(chuàng)建一個(gè)虛擬的生成字段 (僅限 MySQL) |
PHP編程字段更新
PHP編程
Schema::table('users', function (Blueprint $table) {
$table->string('phone',20)->change();
$table->string('username',60)->->nullable()->change();
});
PHP編程重命名字段
PHP編程
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('from', 'to');
});
PHP編程字段移除
PHP編程
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['last_ip', 'last_login']);
});
PHP編程在使用字段更新,重命名字段,字段移除之前,請(qǐng)務(wù)必在你的 composer.json文件require鍵名中添加< "doctrine/dbal": "^2.5">值.然后composer update進(jìn)行更新或
PHP編程
composer require doctrine/dbal
PHP編程創(chuàng)建索引
PHP編程
$table->string('email')->unique();
Command | Description |
---|---|
$table->primary('id'); | 加入主鍵. |
$table->primary(['first', 'last']); | 加入復(fù)合鍵. |
$table->unique('email'); | 加入唯一索引. |
$table->unique('state', 'my_index_name'); | 自定義索引名稱. |
$table->unique(['first', 'last']); | 加入復(fù)合唯一鍵. |
$table->index('state'); | 加入基本索引. |
PHP編程開(kāi)啟和關(guān)閉外鍵約束
PHP編程
Schema::enableForeignKeyConstraints();
Schema::disableForeignKeyConstraints();
PHP編程運(yùn)行遷移
PHP編程
php artisan migrate
PHP編程在線上環(huán)境強(qiáng)制執(zhí)行遷移
PHP編程
php artisan migrate --force
PHP編程回滾遷移
PHP編程若要回滾最后一次遷移,則可以使用 rollback 命令.此命令是對(duì)上一次執(zhí)行的「批量」遷移回滾,其中可能包括多個(gè)遷移文件:
PHP編程
php artisan migrate:rollback
PHP編程在 rollback 命令后加上 step 參數(shù),你可以限制回滾遷移的個(gè)數(shù).例如,下面的命令將會(huì)回滾最后的 5 個(gè)遷移.
PHP編程
php artisan migrate:rollback --step=5
PHP編程migrate:reset 命令可以回滾應(yīng)用程序中的所有遷移:
PHP編程
php artisan migrate:reset
PHP編程使用單個(gè)命令來(lái)執(zhí)行回滾或遷移
PHP編程migrate:refresh 命令不僅會(huì)回滾數(shù)據(jù)庫(kù)的所有遷移還會(huì)接著運(yùn)行 migrate 命令.所以此命令可以有效的重新創(chuàng)建整個(gè)數(shù)據(jù)庫(kù):
PHP編程
php artisan migrate:refresh
// 刷新數(shù)據(jù)庫(kù)結(jié)構(gòu)并執(zhí)行數(shù)據(jù)填充
php artisan migrate:refresh --seed
PHP編程使用 refresh 命令并加上 step 參數(shù),你也可以限制執(zhí)行回滾和再遷移的個(gè)數(shù).比如,下面的命令會(huì)回滾并再遷移最后的 5 個(gè)遷移:
PHP編程
php artisan migrate:refresh --step=5
PHP編程無(wú)法生成遷移文件
PHP編程在 Laravel 項(xiàng)目中,由于測(cè)試,有時(shí)候用 PHP artisan make:migration create_xxx_table 創(chuàng)建數(shù)據(jù)庫(kù)遷移.如果把創(chuàng)建的遷移文件 database/migrations/2017_07_30_133748_create_xxx_table.php 文件給刪除了,再次執(zhí)行 php artisan make:migration create_xxx_table 會(huì)報(bào)錯(cuò):
PHP編程重新運(yùn)行 composer update 又可以執(zhí)行上面的命令了.
PHP編程以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持維易PHP.
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/400.html