加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Laravel 的数据库迁移的方法

发布时间:2020-12-14 20:02:39 所属栏目:大数据 来源:网络整理
导读:本文介绍了Laravel 的数据库迁移的方法,分享给大家,具体如下: 生成迁移 --table 和 --create 选项可用来指定数据表的名称,或是该迁移被执行时会创建的新数据表。这些选项需在预生成迁移文件时填入指定的数据表: 添加字段 databasemigrations2017_07_

本文介绍了Laravel 的数据库迁移的方法,分享给大家,具体如下:

生成迁移

--table 和 --create 选项可用来指定数据表的名称,或是该迁移被执行时会创建的新数据表。这些选项需在预生成迁移文件时填入指定的数据表:

添加字段

databasemigrations2017_07_30_133748_create_users_table.php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateUsersTable extends Migration
{
/**

  • 运行数据库迁移
  • @return void
    */
    public function up()
    {
    //
    Schema::create('users',function (Blueprint $table){
    $table->increments('id')->comment('递增ID');
    $table->string('email',60)->comment('会员Email');
    $table->string('phone',20)->comment('会员手机号');
    $table->string('username',60)->comment('用户名');
    $table->string('password',32)->comment('用户密码');
    $table->char('rank',10)->comment('会员等级');
    $table->unsignedSmallInteger('sex')->comment('性别;0保密;1男;2女');
    $table->unsignedSmallInteger('status')->comment('用户状态');
    $table->ipAddress('last_ip')->default('0.0.0.0')->comment('最后一次登录IP');
    $table->timeTz('last_login')->comment('最后一次登录时间');
    $table->timestamps();
    });
    }

/**

  • 回滚数据库迁移
  • @return void
    */
    public function down()
    {
    //
    Schema::drop('users');
    }
    }

要创建一张新的数据表,可以使用 Schema facade 的 create 方法。create 方法接收两个参数:第一个参数为数据表的名称,第二个参数为一个 闭包 ,此闭包会接收一个用于定义新数据表的 Blueprint 对象

你可以方便地使用 hasTable 和 hasColumn 方法来检查数据表或字段是否存在:

如果你想要在一个非默认的数据库连接中进行数据库结构操作,可以使用 connection 方法:

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

你可以在数据库结构构造器上设置 engine 属性来设置数据表的存储引擎:

engine = 'InnoDB'; $table->increments('id'); });

重命名与删除数据表

//删除已存在的数据表
Schema::drop('users');
Schema::dropIfExists('users');

创建字段

string('email'); });

bigIncrements('id');bigInteger('votes');binary('data');boolean('confirmed');char('name',4);date('created_at');dateTime('created_at');dateTimeTz('created_at');decimal('amount',5,2);double('column',15,8);enum('choices',['foo','bar']);float('amount',8,2);increments('id');integer('votes');ipAddress('visitor');json('options');jsonb('options');longText('description');macAddress('device');mediumIncrements('id');mediumInteger('numbers');mediumText('description');morphs('taggable');nullableMorphs('taggable');nullableTimestamps();rememberToken();smallIncrements('id');smallInteger('votes');softDeletes();string('email');string('name',100);text('description');time('sunrise');timeTz('sunrise');tinyInteger('numbers');timestamp('added_on');timestampTz('added_on');timestamps();timestampsTz();unsignedBigInteger('votes');unsignedInteger('votes');unsignedMediumInteger('votes');unsignedSmallInteger('votes');unsignedTinyInteger('votes');uuid('id');

字段修饰

string('email')->nullable(); });

Modifier

after('column')comment('my comment')default($value)first()nullable()storedAs($expression)unsigned()virtualAs($expression)

字段更新

string('phone',20)->change(); $table->string('username',60)->->nullable()->change(); });

重命名字段

renameColumn('from','to'); });

字段移除

dropColumn(['last_ip','last_login']); });

在使用字段更新,重命名字段,字段移除之前,请务必在你的 composer.json文件require键名中添加< "doctrine/dbal": "^2.5">值。然后composer update进行更新或

创建索引

string('email')->unique();

描述
Description
primary('id');primary(['first','last']);unique('email');unique('state','my_index_name');unique(['first','last']);index('state');

开启和关闭外键约束

运行迁移

在线上环境强制执行迁移

回滚迁移

若要回滚最后一次迁移,则可以使用 rollback 命令。此命令是对上一次执行的「批量」迁移回滚,其中可能包括多个迁移文件:

在 rollback 命令后加上 step 参数,你可以限制回滚迁移的个数。例如,下面的命令将会回滚最后的 5 个迁移。

migrate:reset 命令可以回滚应用程序中的所有迁移:

使用单个命令来执行回滚或迁移

migrate:refresh 命令不仅会回滚数据库的所有迁移还会接着运行 migrate 命令。所以此命令可以有效的重新创建整个数据库:

使用 refresh 命令并加上 step 参数,你也可以限制执行回滚和再迁移的个数。比如,下面的命令会回滚并再迁移最后的 5 个迁移:

无法生成迁移文件

在 Laravel 项目中,由于测试,有时候用 PHP artisan make:migration create_xxx_table 创建数据库迁移。如果把创建的迁移文件 database/migrations/2017_07_30_133748_create_xxx_table.php 文件给删除了,再次执行 php artisan make:migration create_xxx_table 会报错:

代码如下:

重新运行 composer update 又可以执行上面的命令了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

Description