Laravel 数据迁移

数据迁移

1. 生成迁移

生成的迁移文件会存放在 database/migration目录下

php artisan make:migration create_users_table
# --tbale 确定表名
php artisan make:migration create_users_table --table=users
# --create 是否在迁移过程中产生新的数据表
php artisan make:migration create_users_table -- create=users

2. 迁移转储

# 转储但不删除源迁移文件
php artisan schema:dump
# 转储并删除源迁移文件
php artisan schema:dump --prune

3.迁移操作

# 执行未被执行的迁移
php artisan migrate
# 在生产环境强制迁移 => 是破坏性的 可以回导致数据丢失
php artisan migrate --force
# 回滚迁移 => 回滚到最后一批迁移
php artisan migrate:rollback
# --step 回滚指定数量的迁移
php artisan migrate:roolback --step=4
# 重置回上一次数据迁移状态
php artisan migrate:reset

4. 迁移的操作方法

在生成迁移文件后会有两个方法

<?php
​
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
​
class CreateUsersTable extends Migration
{
    # up : 向数据库中新增表或列或索引
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }
    # down : 与up方法相反
    public function down()
    {
        Schema::dropIfExists('users');
    }
}
​

5. 数据表

# 创建数据表 Schema::create('表名' , Blueprint对象)
Schema::create('users' , function(Blueprint $table){
    $table->id();
    $table->string('username');
    $table->string('password');
    $table->timestamps();
})
​
# 判断表是否存在 Schema::hasTable('表名')
if(Schema::hasTable('users')){
    // 表存在操作
}
# 判断字段是否存在 Schema::hasColumn('表名'  , '字段')
if(Schema::hasColumn('users' , 'username'){
    // 字段存在操作   
})
    
# 重命名数据表 Schema::rename('原表名' , '修改表名') => 外键约束还是引用旧的表名
Schema::rename('users' , 'admins');
# 删除数据表
Schema::drop('users');

6. 字段

# 创建字段 Schema facade 的 table 方法
Schema::create('users', function (Blueprint $table) {
    $table->string('username');
});

常用的字段类型

$table->id();               # 递增ID(主键)
$table->foreignId();        # 外键
$table->char();             # char类型 参数二为长度
$table->date();             # date类型 
$table->year();             # year类型
$table->time();             # time类型
$table->timestamp();        # timestamp类型
$table->datetime();         # datetime类型
$table->double();           # double类型 参数二为总位数 参数三为小数位数
$table->enum();             # enum类型 参数二为可选参数 数据类型为数组
$table->integer();          # int类型
$table->text();             # text类型
$table->timestamps();       # 添加可以为空的created_at 和 updated_at
常用的字段修饰
# composer require doctrine/dbal
# Doctrine DBAL库 确定字段当前状态
->autoIncrement();      # 将int类型的字段设置为递增的主键
->charset();            # 设置字符集
->comment();            # 注释
->default();            # 默认值
->from();               # 给自增字段设置起始值
->nullable();           # 允许为空
# composer require doctrine/dbal => 先决条件

# 修改字段 change
Schema::table('users' , function (Blueprint $table){
    # 将字段username设置为可以为空
    $table->string('username')->nullable()->change();
})

# 重命名字段 renameColumn('原字段名' , '修改字段名')
Schema::table('users' . function (Blueprint $table){
    $table->renameColunm('username' , 'name');
})

# 删除字段 dropColumn('字段名')
Schema::table('users' , function (Blueprint $table){
    $table->dropColumn('username');
    # 也可以传入数组一次删除多个字段
    $table->dropColumn(['username' , 'password']);
})

laravel 数据迁移文档 : 数据库迁移 | 数据库 |《Laravel 8 中文文档 8.x》| Laravel China 社区

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值