且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel迁移在现有数据库表中添加colum问题

更新时间:2021-12-26 07:12:41

您需要按照以下步骤使用laravel迁移在现有表中添加列.

You need to follow the following steps for adding a column in existing table using laravel migrations.

Laravel 3 +,4 +

  1. 使用CLI进行迁移(使用特定名称以避免与现有模型冲突)

  1. Make a migration using CLI as (Use a specific name to avoid ***ing with existing models)

php artisan migrate:make add_dob_users_table --table=users

Laravel 5 +

  1. 使用CLI进行迁移(使用特定名称以避免与现有模型冲突)

  1. Make a migration using CLI as (Use a specific name to avoid ***ing with existing models)

php artisan make:migration add_dob_to_users

Laravel 3 +

  1. 使用Schema::table()方法(在访问现有表时,不创建新表),并添加如下列:

  1. Use the Schema::table() method (As you're accessing an existing table, not creating a new one) and add a column like this:

public function up()
{
    Schema::table('users', function($table) {
        $table->string('dob')->nullable()->change();
    });
}

  • 添加回滚选项:

  • Add the rollback option:

    public function down()
    {
        Schema::table('users', function($table) {
            $table->dropColumn('dob');
        });
    }
    

  • Laravel 4 +,5 +

    1. 使用Schema::table()方法(在访问现有表时,不创建新表),并添加如下列:

    1. Use the Schema::table() method (As you're accessing an existing table, not creating a new one) and add a column like this:

    public function up()
    {
        Schema::table('users', function(Blueprint $table) {
            $table->string('dob')->nullable()->change();
        });
    }
    

  • 添加回滚选项:

  • Add the rollback option:

    public function down()
    {
        Schema::table('users', function(Blueprint $table) {
            $table->dropColumn('dob');
        });
    }
    

  • Laravel 3 +,4 +,5 +

    1. 最终运行迁移为

    1. Finally run your migrations as

    php artisan migrate

    用于创建和运行迁移的文档是

    Docs for creating and running migrations are

    Laravel 3 +

    Laravel 3 Schema Builder

    Laravel 3迁移

    Laravel 4、5

    Laravel模式生成器

    Laravel迁移