架构构建器
介绍
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'); | 使用“big integer”等效的递增 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', array('foo', 'bar')); | 表的 ENUM 等效 |
$table->float('amount'); | 表的 FLOAT 等效 |
$table->increments('id'); | 表的递增 ID(主键)。 |
$table->integer('votes'); | 表的 INTEGER 等效 |
$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');
重命名列
要重命名列,可以在 Schema 构建器上使用 renameColumn
方法。在重命名列之前,请确保在 composer.json
文件中添加 doctrine/dbal
依赖。
Schema::table('users', function($table)
{
$table->renameColumn('from', 'to');
});
不支持重命名 enum
列类型。
删除列
要删除列,可以在 Schema 构建器上使用 dropColumn
方法。在删除列之前,请确保在 composer.json
文件中添加 doctrine/dbal
依赖。
从数据库表中删除列
Schema::table('users', function($table)
{
$table->dropColumn('votes');
});
从数据库表中删除多个列
Schema::table('users', function($table)
{
$table->dropColumn(array('votes', 'avatar', 'location'));
});
检查存在性
检查表的存在性
您可以使用 hasTable
和 hasColumn
方法轻松检查表或列的存在性:
if (Schema::hasTable('users'))
{
//
}
检查列的存在性
if (Schema::hasColumn('users', 'email'))
{
//
}
添加索引
架构构建器支持几种类型的索引。您可以通过两种方式添加它们。首先,您可以在列定义上流畅地定义它们,或者您可以单独添加它们:
$table->string('email')->unique();
或者,您可以选择在单独的行上添加索引。以下是所有可用索引类型的列表:
命令 | 描述 |
---|---|
$table->primary('id'); | 从“users”表中添加主键 |
$table->primary(array('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');
});