架构构建器
介绍
Laravel 的 Schema
类提供了一种与数据库无关的方式来操作表。它适用于 Laravel 支持的所有数据库,并在所有这些系统中具有统一的 API。
创建和删除表
要创建一个新的数据库表,使用 Schema::create
方法:
Schema::create('users', function($table)
{
$table->increments('id');
});
传递给 create
方法的第一个参数是表的名称,第二个是一个 Closure
,它将接收一个 Blueprint
对象,该对象可用于定义新表。
要重命名现有的数据库表,可以使用 rename
方法:
Schema::rename($from, $to);
要指定架构操作应在哪个连接上进行,请使用 Schema::connection
方法:
Schema::connection('foo')->create('users', function($table)
{
$table->increments('id');
});
要删除表,可以使用 Schema::drop
方法:
Schema::drop('users');
Schema::dropIfExists('users');
添加列
要更新现有表,我们将使用 Schema::table
方法:
Schema::table('users', function($table)
{
$table->string('email');
});
表构建器包含多种列类型,您可以在构建表时使用:
命令 | 描述 |
---|---|
$table->bigIncrements('id'); | 使用“大整数”等价物的递增 ID |
$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 等价物 |
$table->double('column', 15, 8); | 带精度的 DOUBLE 等价物,总共 15 位,小数点后 8 位 |
$table->enum('choices', ['foo', 'bar']); | 表中的 ENUM 等价物 |
$table->float('amount'); | 表中的 FLOAT 等价物 |
$table->increments('id'); | 表中的递增 ID(主键) |
$table->integer('votes'); | 表中的 INTEGER 等价物 |
$table->json('options'); | 表中的 JSON 等价物 |
$table->jsonb('options'); | 表中的 JSONB 等价物 |
$table->longText('description'); | 表中的 LONGTEXT 等价物 |
$table->mediumInteger('numbers'); | 表中的 MEDIUMINT 等价物 |
$table->mediumText('description'); | 表中的 MEDIUMTEXT 等价物 |
$table->morphs('taggable'); | 添加 INTEGER taggable_id 和 STRING 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_at 和 updated_at 列 |
$table->rememberToken(); | 添加 remember_token 作为 VARCHAR(100) NULL |
->nullable() | 指定列允许 NULL 值 |
->default($value) | 声明列的默认值 |
->unsigned() | 将 INTEGER 设置为 UNSIGNED |
在 MySQL 上使用 After
如果您使用的是 MySQL 数据库,可以使用 after
方法指定列的顺序:
$table->string('name')->after('email');
更改列
注意: 在更改列之前,请确保在 composer.json
文件中添加 doctrine/dbal
依赖项。
有时您可能需要修改现有列。例如,您可能希望增加字符串列的大小。change
方法使其变得简单!例如,让我们将 name
列的大小从 25 增加到 50:
Schema::table('users', function($table)
{
$table->string('name', 50)->change();
});
我们还可以将列修改为可为空:
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
依赖项。
从数据库表中删除列
Schema::table('users', function($table)
{
$table->dropColumn('votes');
});
从数据库表中删除多个列
Schema::table('users', function($table)
{
$table->dropColumn(['votes', 'avatar', 'location']);
});
检查存在性
检查表的存在性
您可以使用 hasTable
和 hasColumn
方法轻松检查表或列的存在性:
if (Schema::hasTable('users'))
{
//
}
检查列的存在性
if (Schema::hasColumn('users', 'email'))
{
//
}
添加索引
架构构建器支持多种类型的索引。有两种方法可以添加它们。首先,您可以在列定义上流畅地定义它们,或者您可以单独添加它们:
$table->string('email')->unique();
或者,您可以选择在单独的行上添加索引。以下是所有可用索引类型的列表:
命令 | 描述 |
---|---|
$table->primary('id'); | 添加主键 |
$table->primary(['first', 'last']); | 添加复合键 |
$table->unique('email'); | 添加唯一索引 |
$table->index('state'); | 添加基本索引 |
外键
Laravel 还提供了对向表中添加外键约束的支持:
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
在此示例中,我们声明 user_id
列引用 users
表上的 id
列。请确保首先创建外键列!
您还可以为约束的“删除时”和“更新时”操作指定选项:
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
要删除外键,可以使用 dropForeign
方法。对于外键使用的命名约定与其他索引相似:
$table->dropForeign('posts_user_id_foreign');
在创建引用递增整数的外键时,请记住始终将外键列设为 unsigned
。
删除索引
要删除索引,您必须指定索引的名称。Laravel 默认为索引分配一个合理的名称。只需连接表名、索引中的列名和索引类型即可。以下是一些示例:
命令 | 描述 |
---|---|
$table->dropPrimary('users_id_primary'); | 从“users”表中删除主键 |
$table->dropUnique('users_email_unique'); | 从“users”表中删除唯一索引 |
$table->dropIndex('geo_state_index'); | 从“geo”表中删除基本索引 |
删除时间戳和软删除
要删除 timestamps
、nullableTimestamps
或 softDeletes
列类型,可以使用以下方法:
命令 | 描述 |
---|---|
$table->dropTimestamps(); | 从表中删除 created_at 和 updated_at 列 |
$table->dropSoftDeletes(); | 从表中删除 deleted_at 列 |
存储引擎
要为表设置存储引擎,请在架构构建器上设置 engine
属性:
Schema::create('users', function($table)
{
$table->engine = 'InnoDB';
$table->string('email');
});