欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Laravel 的數(shù)據(jù)庫遷移的方法

 更新時間:2017年07月31日 14:18:12   作者:zhiqiangw  
本篇文章主要介紹了Laravel 的數(shù)據(jù)庫遷移的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了Laravel 的數(shù)據(jù)庫遷移的方法,分享給大家,具體如下:

生成遷移

--table 和 --create 選項可用來指定數(shù)據(jù)表的名稱,或是該遷移被執(zhí)行時會創(chuàng)建的新數(shù)據(jù)表。這些選項需在預(yù)生成遷移文件時填入指定的數(shù)據(jù)表:

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
{
  /**
   * 運行數(shù)據(jù)庫遷移
   *
   * @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('會員手機(jī)號');
        $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('用戶狀態(tài)');
        $table->ipAddress('last_ip')->default('0.0.0.0')->comment('最后一次登錄IP');
        $table->timeTz('last_login')->comment('最后一次登錄時間');
        $table->timestamps();
      });
  }

  /**
   * 回滾數(shù)據(jù)庫遷移
   *
   * @return void
   */
  public function down()
  {
    //
    Schema::drop('users');
  }
}

要創(chuàng)建一張新的數(shù)據(jù)表,可以使用 Schema facade 的 create 方法。create 方法接收兩個參數(shù):第一個參數(shù)為數(shù)據(jù)表的名稱,第二個參數(shù)為一個 閉包 ,此閉包會接收一個用于定義新數(shù)據(jù)表的 Blueprint 對象

你可以方便地使用 hasTable 和 hasColumn 方法來檢查數(shù)據(jù)表或字段是否存在:

if (Schema::hasTable('users')) {
  //
}
if (Schema::hasColumn('users', 'email')) {
  //
}

如果你想要在一個非默認(rèn)的數(shù)據(jù)庫連接中進(jìn)行數(shù)據(jù)庫結(jié)構(gòu)操作,可以使用 connection 方法:

Schema::connection('foo')->create('users', function (Blueprint $table) {
  $table->increments('id');
});

你可以在數(shù)據(jù)庫結(jié)構(gòu)構(gòu)造器上設(shè)置 engine 屬性來設(shè)置數(shù)據(jù)表的存儲引擎:

Schema::create('users', function (Blueprint $table) {
  $table->engine = 'InnoDB';
  $table->increments('id');
});

重命名與刪除數(shù)據(jù)表

Schema::rename($from, $to);//重命名

//刪除已存在的數(shù)據(jù)表
Schema::drop('users');
Schema::dropIfExists('users');

創(chuàng)建字段

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),并帶有長度。
$table->date('created_at'); 相當(dāng)于 DATE 型態(tài)
$table->dateTime('created_at'); 相當(dāng)于 DATETIME 型態(tài)。
$table->dateTimeTz('created_at'); DATETIME (帶時區(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ù)點后面有 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ù)點后面有 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),并帶有長度。
$table->text('description'); 相當(dāng)于 TEXT 型態(tài)。
$table->time('sunrise'); 相當(dāng)于 TIME 型態(tài)。
$table->timeTz('sunrise'); 相當(dāng)于 TIME (帶時區(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 (帶時區(qū)) 形態(tài)。
$table->timestamps(); 加入 created_at 和 updated_at 字段。
$table->timestampsTz(); 加入 created_at and updated_at (帶時區(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)。

字段修飾

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)建一個存儲的生成字段 (僅限 MySQL)
->unsigned() 設(shè)置 integer 字段為 UNSIGNED
->virtualAs($expression) 創(chuàng)建一個虛擬的生成字段 (僅限 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']);
});

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

composer require doctrine/dbal

創(chuàng)建索引

$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'); 加入基本索引。

開啟和關(guān)閉外鍵約束

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

運行遷移

php artisan migrate

在線上環(huán)境強(qiáng)制執(zhí)行遷移

php artisan migrate --force

回滾遷移

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

php artisan migrate:rollback

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

php artisan migrate:rollback --step=5

migrate:reset 命令可以回滾應(yīng)用程序中的所有遷移:

php artisan migrate:reset

使用單個命令來執(zhí)行回滾或遷移

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

php artisan migrate:refresh
// 刷新數(shù)據(jù)庫結(jié)構(gòu)并執(zhí)行數(shù)據(jù)填充
php artisan migrate:refresh --seed

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

php artisan migrate:refresh --step=5

無法生成遷移文件

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

復(fù)制代碼 代碼如下:

[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 又可以執(zhí)行上面的命令了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論