PHP学习:Laravel 的数据库迁移的方法
《PHP学习:Laravel 的数据库迁移的方法》要点: PHP编程本文介绍了Laravel 的数据库迁移的方法,分享给大家,具体如下: PHP编程生成迁移 PHP编程--table 和 --create 选项可用来指定数据表的名称,或是该迁移被执行时会创建的新数据表.这些选项需在预生成迁移文件时填入指定的数据表: PHP编程 php artisan make:migration create_users_table php artisan make:migration create_users_table --create=users php artisan make:migration add_votes_to_users_table --table=users PHP编程添加字段 PHP编程databasemigrations2017_07_30_133748_create_users_table.php PHP编程 <?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'); } } PHP编程要创建一张新的数据表,可以使用 Schema facade 的 create 方法.create 方法接收两个参数:第一个参数为数据表的名称,第二个参数为一个 闭包,此闭包会接收一个用于定义新数据表的 Blueprint 对象 PHP编程你可以方便地使用 hasTable 和 hasColumn 方法来检查数据表或字段是否存在: PHP编程 if (Schema::hasTable('users')) { // } if (Schema::hasColumn('users','email')) { // } PHP编程如果你想要在一个非默认的数据库连接中进行数据库结构操作,可以使用 connection 方法: PHP编程 Schema::connection('foo')->create('users',function (Blueprint $table) { $table->increments('id'); }); PHP编程你可以在数据库结构构造器上设置 engine 属性来设置数据表的存储引擎: PHP编程 Schema::create('users',function (Blueprint $table) { $table->engine = 'InnoDB'; $table->increments('id'); }); PHP编程重命名与删除数据表 PHP编程 Schema::rename($from,$to);//重命名 //删除已存在的数据表 Schema::drop('users'); Schema::dropIfExists('users'); PHP编程创建字段 PHP编程 Schema::table('users',function (Blueprint $table) { $table->string('email'); }); PHP编程字段修饰 PHP编程 Schema::table('users',function (Blueprint $table) { $table->string('email')->nullable(); }); PHP编程字段更新 PHP编程 Schema::table('users',function (Blueprint $table) { $table->string('phone',20)->change(); $table->string('username',60)->->nullable()->change(); }); PHP编程重命名字段 PHP编程 Schema::table('users',function (Blueprint $table) { $table->renameColumn('from','to'); }); PHP编程字段移除 PHP编程 Schema::table('users',function (Blueprint $table) { $table->dropColumn(['last_ip','last_login']); }); PHP编程在使用字段更新,重命名字段,字段移除之前,请务必在你的 composer.json文件require键名中添加< "doctrine/dbal": "^2.5">值.然后composer update进行更新或 PHP编程 composer require doctrine/dbal PHP编程创建索引 PHP编程 $table->string('email')->unique(); PHP编程开启和关闭外键约束 PHP编程 Schema::enableForeignKeyConstraints(); Schema::disableForeignKeyConstraints(); PHP编程运行迁移 PHP编程 php artisan migrate PHP编程在线上环境强制执行迁移 PHP编程 php artisan migrate --force PHP编程回滚迁移 PHP编程若要回滚最后一次迁移,则可以使用 rollback 命令.此命令是对上一次执行的「批量」迁移回滚,其中可能包括多个迁移文件: PHP编程 php artisan migrate:rollback PHP编程在 rollback 命令后加上 step 参数,你可以限制回滚迁移的个数.例如,下面的命令将会回滚最后的 5 个迁移. PHP编程 php artisan migrate:rollback --step=5 PHP编程migrate:reset 命令可以回滚应用程序中的所有迁移: PHP编程 php artisan migrate:reset PHP编程使用单个命令来执行回滚或迁移 PHP编程migrate:refresh 命令不仅会回滚数据库的所有迁移还会接着运行 migrate 命令.所以此命令可以有效的重新创建整个数据库: PHP编程 php artisan migrate:refresh // 刷新数据库结构并执行数据填充 php artisan migrate:refresh --seed PHP编程使用 refresh 命令并加上 step 参数,你也可以限制执行回滚和再迁移的个数.比如,下面的命令会回滚并再迁移最后的 5 个迁移: PHP编程 php artisan migrate:refresh --step=5 PHP编程无法生成迁移文件 PHP编程在 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 会报错: 代码如下:[ErrorException] include(E:laravervendorcomposer/../../database/migrations/2017_07_30_133748_create_users_table.php): failed to open stream: No such file or directory PHP编程重新运行 composer update 又可以执行上面的命令了. PHP编程以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家PHP. (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |