在迁移中向现有表添加新列

问题描述 投票:187回答:9

我无法弄清楚如何使用Laravel框架将新列添加到现有数据库表中。

我尝试使用...编辑迁移文件

<?php

public function up()
{
    Schema::create('users', function ($table) {
        $table->integer("paid");
    });
}

在终端,我执行php artisan migrate:installmigrate

如何添加新列?

php laravel laravel-4 laravel-3 laravel-migrations
9个回答
427
投票

要创建迁移,可以在Artisan CLI上使用migrate:make命令。使用特定名称可避免与现有模型发生冲突

对于Laravel 3:

php artisan migrate:make add_paid_to_users

对于Laravel 5+:

php artisan make:migration add_paid_to_users

然后,您需要使用Schema::table()方法(因为您正在访问现有表,而不是创建新表)。你可以添加这样的列:

public function up()
{
    Schema::table('users', function($table) {
        $table->integer('paid');
    });
}

并且不要忘记添加回滚选项:

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

然后,您可以运行迁移:

php artisan migrate

Laravel 3的文档中都详细介绍了这一点:

对于Laravel 4 / Laravel 5:

编辑:

使用$table->integer('paid')->after('whichever_column');在特定列之后添加此字段。


51
投票

我将使用Laravel 5.1及其后的版本为未来读者添加mike3875的答案。

为了使事情更快,您可以使用标志“--table”,如下所示:

php artisan make:migration add_paid_to_users --table="users"

这将自动添加updown方法内容:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('users', function (Blueprint $table) {
        //
    });
}

类似地,您可以在创建新迁移时使用--create["table_name"]选项,这将为迁移添加更多样板。小点,但在加载它们时很有帮助!


24
投票

如果您使用的是Laravel 5,那么命令就是;

php artisan make:migration add_paid_to_users

所有用于制作的命令(控制器,模型,迁移等)都已在make:命令下移动。

php artisan migrate仍然是相同的。


17
投票

您可以在初始Schema::create方法中添加新列,如下所示:

Schema::create('users', function($table) {
    $table->integer("paied");
    $table->string("title");
    $table->text("description");
    $table->timestamps();
});

如果您已经创建了表,则可以通过创建新迁移并使用Schema::table方法向该表添加其他列:

Schema::table('users', function($table) {
    $table->string("title");
    $table->text("description");
    $table->timestamps();
});

关于这一点的文档是相当彻底的,并没有从version 3version 4变化太多。


12
投票

laravel 5.6 and above

in case you want to add new column as a FOREIGN KEY to an existing table.

通过执行以下命令创建新迁移:make:migration

示例:

php artisan make:migration add_store_id_to_users_table --table=users

在database / migrations文件夹中,您有新的迁移文件,例如:

2018_08_08_093431_add_store_id_to_users_table.php(见评论)

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddStoreIdToUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {

            // 1. Create new column
            // You probably want to make the new column nullable
            $table->integer('store_id')->unsigned()->nullable()->after('password');

            // 2. Create foreign key constraints
            $table->foreign('store_id')->references('id')->on('stores')->onDelete('SET NULL');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {

            // 1. Drop foreign key constraints
            $table->dropForeign(['store_id']);

            // 2. Drop the column
            $table->dropColumn('store_id');
        });
    }
}

之后运行命令:

php artisan migrate

如果您因任何原因要撤消上次迁移,请运行以下命令:

php artisan migrate:rollback

您可以在docs中找到有关迁移的更多信息


7
投票

您只需修改现有的迁移文件,例如在表中添加一列,然后在终端中键入:

$ php artisan migrate:refresh

4
投票

这个东西是在laravel 5.1上工作的。

首先,在您的终端上执行此代码

php artisan make:migration add_paid_to_users --table=users

之后转到项目目录并展开目录数据库 - 迁移和编辑文件add_paid_to_users.php,添加此代码

public function up()
{
    Schema::table('users', function (Blueprint $table) {
         $table->string('paid'); //just add this line
    });
}

之后返回终端并执行此命令

php artisan migrate

希望这个帮助。


2
投票

首先回滚您之前的迁移

php artisan migrate:rollback

之后,您可以修改现有的迁移文件(添加新的,重命名或删除列),然后重新运行迁移文件

php artisan migrate

0
投票

虽然迁移文件是其他人提到的最佳实践,但在紧要关头,您还可以添加带修补程序的列。

$ php artisan tinker

以下是终端的单行示例:

Schema::table('users', function(\Illuminate\Database\Schema\Blueprint $table){ $table->integer('paid'); })

(这里的格式是为了便于阅读)

Schema::table('users', function(\Illuminate\Database\Schema\Blueprint $table){ 
    $table->integer('paid'); 
});
© www.soinside.com 2019 - 2024. All rights reserved.