鍍金池/ 教程/ PHP/ 結構生成器
Laravel Cashier
Eloquent ORM
HTTP 響應
發(fā)行說明
擴展包開發(fā)
HTTP 控制器
事件
擴展框架
Contracts
開發(fā)
配置
表單驗證
錯誤與日志
Hashing
貢獻指南
郵件
Session
遷移與數(shù)據(jù)填充
查詢構造器
Redis
升級向導
概覽
緩存
服務提供者
Envoy 任務執(zhí)行器
隊列
單元測試
服務容器
文件系統(tǒng) / 云存儲
認證
請求的生命周期
加密
模板
視圖 (View)
Laravel Homestead
Laravel 安裝指南
介紹
Command Bus
分頁
輔助方法
應用程序結構
HTTP 路由
HTTP 請求
基本用法
本地化
HTTP 中間件
結構生成器
Facades
Laravel Elixir

結構生成器

介紹

Laravel 的結構生成器 (Schema) 提供一個與數(shù)據(jù)庫無關的數(shù)據(jù)表產生方法,它可以很好的處理 Laravel 支持的各種數(shù)據(jù)庫類型,并且在不同系統(tǒng)間提供一致性的 API 操作。

建立與刪除數(shù)據(jù)表

要建立一個新的數(shù)據(jù)表,可使用Schema::create 方法:

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

傳入 create 方法的第一個參數(shù)是數(shù)據(jù)表名稱,第二個參數(shù)是 Closure 并接收Blueprint 對象被用來定義新的數(shù)據(jù)表。

要修改數(shù)據(jù)表名稱,可使用 rename 方法:

Schema::rename($from, $to);

要指定特定連接來操作,可使用 Schema::connection 方法:

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

要移除數(shù)據(jù)表,可使用 Schema::drop 方法:

Schema::drop('users');

Schema::dropIfExists('users');

加入字段

更新現(xiàn)有的數(shù)據(jù)表,可使用 Schema::table 方法:

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

數(shù)據(jù)表產生器提供多種字段類型可使用,在您建立數(shù)據(jù)表時也許會用到:

命令 功能描述
$table->bigIncrements('id'); ID 自動增量,使用相當于「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->decimal('amount', 5, 2); 相當于 DECIMAL 類型,并帶有精度與基數(shù)
$table->double('column', 15, 8); 相當于 DOUBLE 類型,總共有 15 位數(shù),在小數(shù)點后面有 8 位數(shù)
$table->enum('choices', array('foo', 'bar')); 相當于 ENUM 類型
$table->float('amount'); 相當于 FLOAT 類型
$table->increments('id'); 相當于 Incrementing 類型 (數(shù)據(jù)表主鍵)
$table->integer('votes'); 相當于 INTEGER 類型
$table->json('options'); 相當于 JSON 類型
$table->longText('description'); 相當于 LONGTEXT 類型
$table->mediumInteger('numbers'); 相當于 MEDIUMINT 類型
$table->mediumText('description'); 相當于 MEDIUMTEXT 類型
$table->morphs('taggable'); 加入整數(shù) taggable_id 與字串 taggable_type
$table->nullableTimestamps(); timestamps() 相同,但允許 NULL
$table->smallInteger('votes'); 相當于 SMALLINT 類型
$table->tinyInteger('numbers'); 相當于 TINYINT 類型
$table->softDeletes(); 加入 deleted_at 字段于軟刪除使用
$table->string('email'); 相當于 VARCHAR 類型
$table->string('name', 100); 相當于 VARCHAR 類型,并指定長度
$table->text('description'); 相當于 TEXT 類型
$table->time('sunrise'); 相當于 TIME 類型
$table->timestamp('added_on'); 相當于 TIMESTAMP 類型
$table->timestamps(); 加入 created_atupdated_at 字段
$table->rememberToken(); 加入 remember_token 使用 VARCHAR(100) NULL
->nullable() 標示此字段允許 NULL
->default($value) 聲明此字段的默認值
->unsigned() 配置整數(shù)是無分正負

在 MySQL 使用 After 方法

若您使用 MySQL 數(shù)據(jù)庫,您可以使用 after 方法來指定字段的順序:

$table->string('name')->after('email');

修改字段

有時候您需要修改一個存在的字段,例如:您可能想增加保存文本字段的長度。通過 change 方法讓這件事情變得非常容易!假設我們想要將字段 name 的長度從 25 增加到 50 的時候:

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

另外也能將某個字段修改為允許 NULL:

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

修改字段名稱

要修改字段名稱,可在結構生成器內使用 renameColumn 方法,請確認在修改前composer.json 文件內已經加入 doctrine/dbal。

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

注意: 目前暫時還不支持修改表中的結構為 enum 字段類型。

移除字段

要移除字段,可在結構生成器內使用 dropColumn 方法,請確認在移除前composer.json 文件內已經加入 doctrine/dbal。

移除數(shù)據(jù)表字段

Schema::table('users', function($table)
{
    $table->dropColumn('votes');
});

移除數(shù)據(jù)表多個字段

Schema::table('users', function($table)
{
    $table->dropColumn(array('votes', 'avatar', 'location'));
});

檢查是否存在

檢查數(shù)據(jù)表是否存在

您可以輕松的檢查數(shù)據(jù)表或字段是否存在,使用 hasTablehasColumn 方法:

if (Schema::hasTable('users'))
{
    //
}

檢查字段是否存在

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

加入索引

結構生成器支持多種索引類型,有兩種方法可以加入,方法一,您可以在定義字段時順便附加上去,或者是分開另外加入:

$table->string('email')->unique();

或者,您可以獨立一行來加入索引,以下是支持的索引類型:

命令 功能描述
$table->primary('id'); 加入主鍵 (primary key)
$table->primary(array('first', 'last')); 加入復合鍵 (composite keys)
$table->unique('email'); 加入唯一索引 (unique index)
$table->index('state'); 加入基本索引 (index)

外鍵

Laravel 也支持數(shù)據(jù)表的外鍵約束:

$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');

例子中,我們關注字段 user_id 參照到 users 數(shù)據(jù)表的 id 字段。請先確認已經建立外鍵!

您也可以指定選擇在「on delete」和「on update」進行約束動作:

$table->foreign('user_id')
      ->references('id')->on('users')
      ->onDelete('cascade');

要移除外鍵,可使用 dropForeign 方法。外鍵的命名約定如同其他索引:

$table->dropForeign('posts_user_id_foreign');

注意: 當外鍵有參照到自動增量時,記得配置外鍵為 unsigned 類型。

移除索引

要移除索引您必須指定索引名稱,Laravel 默認有脈絡可循的索引名稱。簡單地鏈接這些數(shù)據(jù)表與索引的字段名稱和類型。舉例如下:

命令 功能描述
$table->dropPrimary('users_id_primary'); 從「users」數(shù)據(jù)表移除主鍵
$table->dropUnique('users_email_unique'); 從「users」數(shù)據(jù)表移除唯一索引
$table->dropIndex('geo_state_index'); 從「geo」數(shù)據(jù)表移除基本索引

移除時間戳記和軟刪除

要移除 timestampsnullableTimestampssoftDeletes 字段類型,您可以使用以下方法:

命令 功能描述
$table->dropTimestamps(); 移除 created_atupdated_at 字段
$table->dropSoftDeletes(); 移除 deleted_at 字段

保存引擎

要配置數(shù)據(jù)表的保存引擎,可在結構生成器配置 engine 屬性:

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

    $table->string('email');
});
上一篇:HTTP 請求下一篇:查詢構造器