Laravel 数据库:迁移

  1. 生成迁移
  2. 运行迁移
  3. 重建迁移
  4. 创建数据表
  5. 可用的字段类型
  6. 列修饰符
  7. 索引修饰
  8. 在up中添加对应的代码创建对应的字段

生成迁移

使用 Artisan 命令 make:migration 来创建迁移:

php artisan make:migration create_users_table

新创建的迁移会放在你的 database/migrations 目录。每个迁移的文件名都包含一个时间戳来让 Laravel 确认迁移的顺序。

参数

--table--create 选项也可用于确定表的名称以及是否在迁移中创建新的数据表。这些选项用指定的迁移模板预先填充指定的数据表:

php artisan make:migration create_users_table --create=users

php artisan make:migration add_votes_to_users_table --table=users

运行迁移

请执行 Artisan 的 migrate 命令来执行所有未执行的迁移:

php artisan migrate

重建迁移

使用单个命令来执行迁移和回滚 migrate:refresh 命令将会在回滚你所有的迁移后执行 migrate 命令。这个命令可以高效的重新创建你的整个数据库:

php artisan migrate:refresh

// Refresh the database and run all database seeds...
php artisan migrate:refresh --seed

创建数据表

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

检查表 / 列是否存在
你可以使用 hasTablehasColumn 方法轻松的检查数据表和字段是否存在:

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

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

可用的字段类型

https://laravel.com/docs/7.x/migrations#creating-columns

列修饰符

https://laravel.com/docs/7.x/migrations#column-modifiers

索引修饰

https://laravel.com/docs/7.x/migrations#creating-indexes

在up中添加对应的代码创建对应的字段

命令                         功能描述
$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 类型,并带有精度与基数
$table->double('column', 15, 8);     相当于 DOUBLE 类型,总共有 15 位数,在小数点后面有 8 位数
$table->enum('choices', array('foo', 'bar'));     相当于 ENUM 类型
$table->float('amount');     相当于 FLOAT 类型
$table->increments('id');     相当于 Incrementing 类型 (数据表主键)
$table->integer('votes');     相当于 INTEGER 类型
$table->json('options');     相当于 JSON 类型
$table->jsonb('options');     JSONB equivalent to the table
$table->longText('description');     相当于 LONGTEXT 类型
$table->mediumInteger('numbers');     相当于 MEDIUMINT 类型
$table->mediumText('description');     相当于 MEDIUMTEXT 类型
$table->morphs('taggable');     加入整数 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_at 和 updated_at 字段
$table->rememberToken();     加入 remember_token 使用 VARCHAR(100) NULL
->nullable()     标示此字段允许 NULL
->default($value)     声明此字段的默认值
->unsigned()     配置整数是无分正负
//加入索引
$table->string('email')->unique();
$table->primary('id');
$table->primary(array('first', 'last'));加入复合键
$table->unique('email');
$table->index('state');
//移除索引
$table->dropPrimary('users_id_primary');
$table->dropUnique('users_email_unique');
$table->dropIndex('geo_state_index');
//外键
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
//你也可以指定选择在「on delete」和「on update」进行约束动作
$table->foreign('user_id')
      ->references('id')->on('users')
      ->onDelete('cascade');
//移除外键
$table->dropForeign('posts_user_id_foreign');

//修改或者删除表
//修改表名
Schema::rename($from, $to);
//删除表
Schema::drop('users');
Schema::dropIfExists('users');
//修改字段属性
$table->string('name', 50)->nullable()->change();
//修改字段名称
$table->renameColumn('from', 'to');
//移除字段
$table->dropColumn('votes');
$table->dropColumn(['votes', 'avatar', 'location']);
//检测字段是否存在
if (Schema::hasTable('users')){}
if (Schema::hasColumn('users', 'email')){}
//移除时间戳记和软删除
$table->dropTimestamps();    //移除 created_at 和 updated_at 字段
$table->dropSoftDeletes();    //移除 deleted_at 字段
//修改表索引
Schema::create('users', function($table)
{
    $table->engine = 'InnoDB';

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

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 lzdong@foxmail.com

×

喜欢就点赞,疼爱就打赏