Laravel 数据库:迁移 Demo

public function up()
{
    //如果当前数据中没有 表则创建表
    if(!Schema::hasTable('users')) {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nickname')->comment('昵称');
            $table->string('username')->comment('用户名');
            $table->char('password')->comment('密码');
            $table->bigInteger('money')->default(0)->comment('余额');
            $table->string('email')->comment('邮箱');
            $table->string('phone')->comment('手机号');
            $table->tinyInteger('is_verify_email')->comment('邮箱验证状态')->default('0');
            $table->tinyInteger('is_verify_phone')->comment('手机验证状态')->default('0');
            $table->tinyInteger('status')->comment('状态')->default('0');
            $table->timestamps();
            $table->softDeletes('deleted_at', 0);
            //唯一索引
            $table->unique('username');
            //一般索引
            $table->index('email');
            //设置表的引擎
            $table->engine = 'innodb';
          

        });
    } else {
        // 调整表结构
        Schema::table('users', function ($table) {
            if (Schema::hasColumn('users', 'email')) {
                //
            }
            if(!Schema::hasColumn('users','sex')) {
                $table->tinyInteger('sex')->comment('性别');
            }
                
            if(!Schema::hasColumn('users','age')) {
                $table->integer('age')->default(10);          
            }

        });

    }
}


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

×

喜欢就点赞,疼爱就打赏