如何在 Laravel 5 中创建只向现有表添加一列的迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30891346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:47:47  来源:igfitidea点击:

How to create a migration in Laravel 5 that only adds a column to an existing table

phpmysqllaravelmigrationartisan-migrate

提问by Matthew Fritz

I'm trying to add a number of new column to the generic 'users' table that comes with the Laravel 5 installation.

我正在尝试向 Laravel 5 安装附带的通用“用户”表中添加一些新列。

However I create a new migration that does a Schema::create as follows:

但是,我创建了一个执行 Schema::create 的新迁移,如下所示:

Schema::create('users', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('username');
    $table->string('email')->unique();
    $table->string('password', 60);
    $table->string('avatar');
    $table->string('first_name');
    $table->string('last_name');
    $table->string('nickname');
    $table->rememberToken();
    $table->timestamps();
});

In the above I added the avatar, first_name, last_nameand nicknamecolumns.

在上面我说的avatarfirst_namelast_namenickname列。

My problem is that when I run php artisan migrateit's telling me Nothing to migrate.

我的问题是当我运行时php artisan migrate它告诉我Nothing to migrate.

How do I create an "update" migration?

如何创建“更新”迁移?

回答by Pwner

From http://laravel.com/docs/5.1/migrations#creating-columns

来自http://laravel.com/docs/5.1/migrations#creating-columns

Creating Columns

创建列

To update an existing table, we will use the tablemethod on the Schema facade. Like the createmethod, the tablemethod accepts two arguments: the name of the table and a Closure that receives a Blueprint instance we can use to add columns to the table:

要更新现有表,我们将使用tableSchema 外观上的方法。与该create方法一样,该table方法接受两个参数:表的名称和接收蓝图实例的闭包,我们可以使用它向表中添加列:

Schema::table('users', function ($table) {
    $table->string('email');
});

Also look at this from http://laravel.com/docs/5.1/migrations#creating-columns

也可以从http://laravel.com/docs/5.1/migrations#creating-columns查看

Rollback / Migrate In Single CommandNote: you will loose any data in the DB with this method

在单个命令中回滚/迁移注意:使用此方法您将丢失数据库中的所有数据

The migrate:refresh command will first roll back all of your database migrations, and then run the migrate command. This command effectively re-creates your entire database:

migrate:refresh 命令将首先回滚所有数据库迁移,然后运行 ​​migrate 命令。此命令有效地重新创建了整个数据库:

php artisan migrate:refresh

回答by user3449007

You could also delete the migration row from the migrations table for a particular migration (the users migration usually with a batch of 1). Then run php artisan migrate again.

您还可以从特定迁移的迁移表中删除迁移行(用户迁移通常为一批 1)。然后再次运行 php artisan migrate 。

Each migration is stored in the migrations table (check your database) so to recreate a table you need to delete the migration record for that table (users) in the migrations table

每个迁移都存储在迁移表中(检查您的数据库),因此要重新创建一个表,您需要在迁移表中删除该表(用户)的迁移记录

回答by Riaj Ferdous

General process:

一般流程:

I have lost my 6 hours to solve same problem you faced, but now its like a food to eat.

我已经浪费了 6 个小时来解决您面临的同样问题,但现在它就像是一种食物。

Step 1: Prerequisites:

第 1 步:先决条件:

Before modifying a column, be sure to add the doctrine/dbal dependency to your composer.json file. The Doctrine DBAL library is used to determine the current state of the column and create the SQL queries needed to make the specified adjustments to the column

在修改一个列之前,一定要在你的 composer.json 文件中添加 doctrine/dbal 依赖项。Doctrine DBAL 库用于确定列的当前状态并创建对列进行指定调整所需的 SQL 查询

Step 2: For Creating Columns:

第 2 步:用于创建列:

The table method on the Schema facade may be used to update existing tables. Like the create method, the table method accepts two arguments: the name of the table and a Closure that receives a Blueprint instance you may use to add columns to the table:

Schema 外观上的 table 方法可用于更新现有表。与 create 方法一样,table 方法接受两个参数:表的名称和接收蓝图实例的闭包,您可以使用它向表中添加列:

   Schema::table('users', function (Blueprint $table) {
   $table->string('email');
   });

Step 3 (if needed):

第 3 步(如果需要):

Use the following command in console

在控制台中使用以下命令

php artisan migrate:refresh