最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Laravel中數(shù)據(jù)庫(kù)遷移操作的示例詳解

 更新時(shí)間:2023年02月09日 16:36:03   作者:huaweichenai  
這篇文章主要為大家詳細(xì)介紹了Laravel實(shí)現(xiàn)數(shù)據(jù)庫(kù)遷移操作的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下

一:創(chuàng)建遷移

在laravel中使用make:migration命令來創(chuàng)建遷移

php artisan make:migration create_user_table

執(zhí)行上面的命令后這時(shí)候會(huì)在database/migrations 目錄下生成對(duì)應(yīng)的遷移文件,每個(gè)遷移的文件名都包含一個(gè)時(shí)間戳來讓 Laravel 確認(rèn)遷移的順序

二:遷移結(jié)構(gòu)

一個(gè)遷移類包含兩個(gè)方法: up 和 down。up 方法是用于新增數(shù)據(jù)庫(kù)的數(shù)據(jù)表、字段或者索引的,而 down 方法應(yīng)該與 up 方法的執(zhí)行操作相反。

1:up方法

public function up()
{
    Schema::create('user', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

2:down方法

public function down()
{
    Schema::dropIfExists('user');
}

三:運(yùn)行遷移

php artisan migrate

大多數(shù)遷移操作都是破壞性的,這意味著也許會(huì)丟失數(shù)據(jù)。為了防止有人在生產(chǎn)環(huán)境數(shù)據(jù)中運(yùn)行這些命令,在執(zhí)行這些命令之前將提示你進(jìn)行確認(rèn)。如果要強(qiáng)制遷移命令在沒有提示的情況下運(yùn)行,請(qǐng)使用 --force 參數(shù)

php artisan migrate --force

四:遷移回滾

php artisan migrate:rollback

通過向 rollback 命令加上 step 參數(shù),可以回滾指定數(shù)量的遷移

php artisan migrate:rollback --step=5

migrate:reset 命令將會(huì)滾回你應(yīng)用程序所有的遷移:

php artisan migrate:reset

五:回滾后遷移

migrate:refresh 命令將會(huì)在回滾你所有的遷移后執(zhí)行 migrate 命令。這個(gè)命令可以高效的重新創(chuàng)建你的整個(gè)數(shù)據(jù)庫(kù):

php artisan migrate:refresh
 
// 刷新數(shù)據(jù)庫(kù)并執(zhí)行數(shù)據(jù)庫(kù)填充
php artisan migrate:refresh --seed

六:可用字段類型

在laravel的數(shù)據(jù)庫(kù)遷移中,支持的字段類型有:

命令描述
$table->bigIncrements('id');遞增 ID(主鍵),相當(dāng)于「UNSIGNED BIG INTEGER」
$table->bigInteger('votes');相當(dāng)于 BIGINT
$table->binary('data');相當(dāng)于 BLOB
$table->boolean('confirmed');相當(dāng)于 BOOLEAN
$table->char('name', 100);相當(dāng)于帶有長(zhǎng)度的 CHAR
$table->date('created_at');相當(dāng)于 DATE
$table->dateTime('created_at');相當(dāng)于 DATETIME
$table->dateTimeTz('created_at');相當(dāng)于帶時(shí)區(qū) DATETIME
$table->decimal('amount', 8, 2);相當(dāng)于帶有精度與基數(shù) DECIMAL
$table->double('amount', 8, 2);相當(dāng)于帶有精度與基數(shù) DOUBLE
$table->enum('level', ['easy', 'hard']);相當(dāng)于 ENUM
$table->float('amount', 8, 2);相當(dāng)于帶有精度與基數(shù) FLOAT
$table->geometry('positions');相當(dāng)于 GEOMETRY
$table->geometryCollection('positions');相當(dāng)于 GEOMETRYCOLLECTION
$table->increments('id');遞增的 ID (主鍵),相當(dāng)于「UNSIGNED INTEGER」
$table->integer('votes');相當(dāng)于 INTEGER
$table->ipAddress('visitor');相當(dāng)于 IP 地址
$table->json('options');相當(dāng)于 JSON
$table->jsonb('options');相當(dāng)于 JSONB
$table->lineString('positions');相當(dāng)于 LINESTRING
$table->longText('description');相當(dāng)于 LONGTEXT
$table->macAddress('device');相當(dāng)于 MAC 地址
$table->mediumIncrements('id');遞增 ID (主鍵) ,相當(dāng)于「UNSIGNED MEDIUM INTEGER」
$table->mediumInteger('votes');相當(dāng)于 MEDIUMINT
$table->mediumText('description');相當(dāng)于 MEDIUMTEXT
$table->morphs('taggable');相當(dāng)于加入遞增的 taggable_id 與字符串 taggable_type
$table->uuidMorphs('taggable');相當(dāng)于加入 taggable_id 與字符串 taggable_typeUUID 列。
$table->multiLineString('positions');相當(dāng)于 MULTILINESTRING
$table->multiPoint('positions');相當(dāng)于 MULTIPOINT
$table->multiPolygon('positions');相當(dāng)于 MULTIPOLYGON
$table->nullableMorphs('taggable');相當(dāng)于可空版本的 morphs () 字段
$table->nullableUuidMorphs('taggable');相當(dāng)于可空版本的 uuidMorphs() 字段
$table->nullableTimestamps();相當(dāng)于可空版本的 timestamps() 字段
$table->point('position');相當(dāng)于 POINT
$table->polygon('positions');相當(dāng)于 POLYGON
$table->rememberToken();相當(dāng)于可空版本的 VARCHAR (100) 的 remember_token 字段
$table->set('flavors', ['strawberry', 'vanilla']);相當(dāng)于 SET
$table->smallIncrements('id');遞增 ID(主鍵),相當(dāng)于「UNSIGNED SMALLINT」
$table->smallInteger('votes');相當(dāng)于 SMALLINT
$table->softDeletes();相當(dāng)于為軟刪除添加一個(gè)可空的 deleted_at 字段
$table->softDeletesTz();相當(dāng)于為軟刪除添加一個(gè)可空的 帶時(shí)區(qū)的 deleted_at 字段
$table->string('name', 100);相當(dāng)于帶長(zhǎng)度的 VARCHAR
$table->text('description');相當(dāng)于 TEXT
$table->time('sunrise');相當(dāng)于 TIME
$table->timeTz('sunrise');相當(dāng)于帶時(shí)區(qū)的 TIME
$table->timestamp('added_on');相當(dāng)于 TIMESTAMP
$table->timestampTz('added_on');相當(dāng)于帶時(shí)區(qū)的 TIMESTAMP
$table->timestamps();相當(dāng)于可空的 created_at 和 updated_at TIMESTAMP
$table->timestampsTz();相當(dāng)于可空且?guī)r(shí)區(qū)的 created_at 和 updated_at TIMESTAMP
$table->tinyIncrements('id');相當(dāng)于自動(dòng)遞增 UNSIGNED TINYINT
$table->tinyInteger('votes');相當(dāng)于 TINYINT
$table->unsignedBigInteger('votes');相當(dāng)于 Unsigned BIGINT
$table->unsignedDecimal('amount', 8, 2);相當(dāng)于帶有精度和基數(shù)的 UNSIGNED DECIMAL
$table->unsignedInteger('votes');相當(dāng)于 Unsigned INT
$table->unsignedMediumInteger('votes');相當(dāng)于 Unsigned MEDIUMINT
$table->unsignedSmallInteger('votes');相當(dāng)于 Unsigned SMALLINT
$table->unsignedTinyInteger('votes');相當(dāng)于 Unsigned TINYINT
$table->uuid('id');相當(dāng)于 UUID
$table->year('birth_year');相當(dāng)于 YEAR

七:字段修飾

在laravel的數(shù)據(jù)庫(kù)遷移中,支持的字段修飾符有:

命令描述
->after('column')將此字段放置在其它字段 "之后" (MySQL)
->autoIncrement()將 INTEGER 類型的字段設(shè)置為自動(dòng)遞增的主鍵
->charset('utf8')指定一個(gè)字符集 (MySQL)
->collation('utf8_unicode_ci')指定列的排序規(guī)則 (MySQL/SQL Server)
->comment('my comment')為字段增加注釋 (MySQL)
->default($value)為字段指定 "默認(rèn)" 值
->first()將此字段放置在數(shù)據(jù)表的 "首位" (MySQL)
->nullable($value = true)此字段允許寫入 NULL 值(默認(rèn)情況下)
->storedAs($expression)創(chuàng)建一個(gè)存儲(chǔ)生成的字段 (MySQL)
->unsigned()設(shè)置 INTEGER 類型的字段為 UNSIGNED (MySQL)
->useCurrent()將 TIMESTAMP 類型的字段設(shè)置為使用 CURRENT_TIMESTAMP 作為默認(rèn)值
->virtualAs($expression)創(chuàng)建一個(gè)虛擬生成的字段 (MySQL)
->generatedAs($expression)使用指定的序列生成標(biāo)識(shí)列(PostgreSQL)
->always()定義序列值優(yōu)先于標(biāo)識(shí)列的輸入 (PostgreSQL)
->primary('id')添加主鍵
->primary(['id', 'parent_id'])添加復(fù)合鍵
->unique('email')添加唯一索引
->index('state')添加普通索引
->spatialIndex('location')添加空間索引(不支持 SQLite)
->renameIndex('from', 'to')重命名索引
->dropPrimary('users_id_primary')刪除主鍵
->dropUnique('users_email_unique');刪除唯一索引
>dropIndex('geo_state_index');刪除基本索引
->dropSpatialIndex('geo_location_spatialindex');刪除空間索引(不支持 SQLite)

實(shí)例:

Schema::table('users', function (Blueprint $table) {
    $table->string('email')->nullable();
});

八:修改字段

change 方法可以將現(xiàn)有的字段類型修改為新的類型或修改屬性,例:

Schema::table('users', function (Blueprint $table) {
    $table->string('name', 50)->change();
});

renameColumn方法來重命名字段,依賴于doctrine/dbal拓展,例:

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

九:刪除字段

dropColumn 方法來刪除字段,例:

Schema::table('users', function (Blueprint $table) {
    $table->dropColumn(['votes', 'avatar', 'location']);//刪除votes,avatar,location字段
});

十:索引長(zhǎng)度 & Mysql / MariaDB

Laravel 默認(rèn)使用 utf8mb4 編碼,它支持在數(shù)據(jù)庫(kù)中儲(chǔ)存 emojis 。如果你是在版本低于 5.7.7 的 MySQL 或者版本低于 10.2.2 的 MariaDB 上創(chuàng)建索引,那你就需要手動(dòng)配置數(shù)據(jù)庫(kù)遷移的默認(rèn)字符串長(zhǎng)度。即在 app/Providers/AppServiceProvider 中調(diào)用 Schema::defaultStringLength 方法來配置它

use Illuminate\Support\Facades\Schema;
 
/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Schema::defaultStringLength(191);
}

十一:外鍵約束

Laravel 還支持創(chuàng)建用于在數(shù)據(jù)庫(kù)層中的強(qiáng)制引用完整性的外鍵約束。例如,讓我們?cè)?posts 表上定義一個(gè)引用 users 表的 id 字段的 user_id 字段:

Schema::table('posts', function (Blueprint $table) {
    $table->unsignedBigInteger('user_id');
 
    $table->foreign('user_id')->references('id')->on('users');
});

在遷移文件中使用以下方法來開啟或關(guān)閉外鍵約束

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

到此這篇關(guān)于Laravel中數(shù)據(jù)庫(kù)遷移操作的示例詳解的文章就介紹到這了,更多相關(guān)Laravel數(shù)據(jù)庫(kù)遷移內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

小金县| 霍城县| 金塔县| 易门县| 兴安盟| 丹寨县| 普洱| 永和县| 闸北区| 盐城市| 锡林郭勒盟| 永昌县| 阜城县| 壶关县| 辽中县| 泊头市| 娱乐| 盐池县| 巴林左旗| 共和县| 广安市| 莒南县| 玉田县| 白朗县| 陆川县| 全南县| 盐池县| 武平县| 固原市| 南昌市| 绵阳市| 扎鲁特旗| 乐清市| 迭部县| 睢宁县| 金川县| 隆化县| 栾川县| 大邑县| 房山区| 屏山县|