如何使用迁移更新表的struture - Laravel 8

111 阅读2分钟

在Laravel中使用迁移,你可以在数据库中创建表。

使用它,你也可以更新现有的表结构。

在本教程中, 我将向你展示在Laravel 8中使用迁移来更新表结构的2种方法.

How to Update Table struture using migration - Laravel 8


内容

  1. 数据库配置
  2. 创建表
  3. 刷新迁移
  4. 单一表
  5. 结论

1.数 据库配置

打开.env 文件。

指定主机、数据库名称、用户名和密码。

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tutorial
DB_USERNAME=root
DB_PASSWORD=

2.创 建表

  • 使用迁移创建一个新的表Employees ,并添加一些记录。
php artisan make:migration create_employees_table
  • 现在,从项目根目录导航到database/migration/ 文件夹。
  • 找到一个以create_employees_table 结尾的PHP文件并打开它。
  • up() 方法中定义表的结构。
public function up()
{
    Schema::create('employees', function (Blueprint $table) {
        $table->id();
        $table->string('emp_name');
        $table->string('email');
        $table->string('gender');
        $table->smallInteger('active');
        $table->timestamps();
    });
}
  • 运行迁移
php artisan migrate
  • 表已经被创建,并在其中添加一些记录。

Laravel table with data


3.刷 新迁移程序

  • 从项目根目录导航到database/migration/ 文件夹。
  • 打开你想编辑的表迁移文件。在这里,我正在打开employees 表的迁移。
  • 更新up() 方法中的表结构。
public function up()
{
      Schema::create('employees', function (Blueprint $table) {
             $table->id();
             $table->string('emp_name',80);
             $table->string('email',80);
             $table->string('gender',10); 
             $table->smallInteger('status');
             $table->timestamps();
      });
}
  • 在这里,我做了以下改动 -
    • 设置字符串数据类型的字段长度。
    • 添加一个新的列status ,并
    • 删除active 列。
  • 刷新迁移-
php artisan migrate:refresh

**注意 -**上述命令会重新创建整个数据库并删除所有数据。

Laravel table after migration:refresh execution

  • 或者,你可以从上一次迁移中重新迁移特定数量的数据------。
php artisan migrate:refresh --step=2
  • 上面的命令只回滚并重新迁移最后两次迁移。

4.单 一表

需要doctrine/dbal 包来修改现有的列 -

composer require doctrine/dbal

创建迁移 -

php artisan make:migration update_and_addstatus_to_employees_table
  • 现在,从项目根目录导航到database/migration/ 文件夹。
  • 找到一个以update_and_addstatus_to_employees_table 结尾的PHP文件并打开它。
  • up() 方法中定义表的结构。
public function up()
{
       Schema::table('employees', function (Blueprint $table) {
               $table->renameColumn('emp_name', 'employee_name');// Renaming "emp_name" to "employee_name"
               $table->string('gender',10)->change(); // Change Datatype length
               $table->dropColumn('active'); // Remove "active" field
               $table->smallInteger('status')->after('email'); // Add "status" column
       });
}
  • 在这里,我做了以下改动 -
    • emp_name 列的名称重命名为employee_name
    • 改变了gender 列的数据类型长度。
    • 删除active 列。
    • 添加一个新的status 列。
  • down() 方法中 -
public function down()
{
        Schema::table('employees', function (Blueprint $table) {
               $table->renameColumn('employee_name', 'emp_name');
               $table->string('gender')->change(); 
               $table->smallInteger('active');
               $table->dropColumn('status');
        });
}
  • 运行迁移 -
php artisan migrate
  • 表结构将被更新而不会丢失数据。



5.总 结

你可以根据你的要求使用上述任何一种方法。

如果你想在不丢失数据的情况下改变表结构,那么请使用第二种方法。

只有当你想重新创建整个数据库时才使用migrate:refresh ,或者使用--step ,只重新迁移特定数量的数据库。

如果你觉得这个教程有帮助,那么别忘了分享。

相关帖子。

[

用PHP和MySQL创建简单的登录页面

](makitweb.com/create-simp…

如何用AngularJS从MySQL获取数据--PHP

](makitweb.com/how-to-get-…

如何用jQuery隐藏和显示表的列

](makitweb.com/how-to-hide…)