我不知道如何使用Laravel框架向现有的数据库表中添加新列。

我试图编辑迁移文件使用…

<?php

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

在终端中,我执行了php artisan migrate:install和migrate。

如何添加新列?


当前回答

步骤1

php artisan make:migration add_sex_to_users_table --table=users

步骤2

在新生成的迁移文件中,您将发现up和down钩子方法。在上勾中,添加你想要添加的列,在下勾中,添加你需要删除的列。例如,我需要在用户列上添加性,所以我将在向上钩添加下面的行。

$table->integer('quantity')->default(1)->nullable();

就像这样

public function up()
{
    Schema::table('service_subscriptions', function (Blueprint $table) {
        $table->integer('quantity')->default(1)->nullable();
    });
}

步骤3

执行如下迁移命令

php artisan migrate

然后您将添加一个新列

其他回答

尽管正如其他人所提到的,迁移文件是最佳实践,但在必要时,您也可以使用tinker添加一个列。

$ 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'); 
});

在拉拉维尔 8

php artisan make:migration add_columnname_to_tablename_table --table=tablename

然后在创建迁移之后

public function up()
    {
        Schema::table('users', function (Blueprint $table) {

            // 1. Create new column
            $table->datatype('column_name')->nullable();
        });
    }
public function down()
    {
        Schema::table('users', function (Blueprint $table) {

            // 1. Create new column
            $table->dropColumn('column_name');
        });
    }

然后运行

php artisan migrate

如果遇到错误,则用创建表之前的日期重命名迁移名称,然后再次运行PHP artisan migrate

如果您正在使用Laravel 5,命令将是;

php artisan make:migration add_paid_to_users

所有用于制作东西的命令(控制器、模型、迁移等)都被移动到make:命令下。

PHP工匠迁移仍然是一样的。

如果您想将新列作为外键添加到现有表。

执行命令: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

您可以在文档中找到更多关于迁移的信息

我将添加到mike3875的答案,供将来使用Laravel 5.1及后续版本的读者使用。

为了让事情变得更快,你可以像这样使用标志"——table":

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

这将自动添加up和down方法内容:

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

类似地,您可以在创建新的迁移时使用——create["table_name"]选项,这将为您的迁移添加更多的样板。这是个小问题,但在做大量的事情时很有帮助!