使用迁移重命名 Laravel 中的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51130611/
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 17:51:44  来源:igfitidea点击:

rename column in laravel using migration

laravelmigration

提问by Mostafa Norzade

I use laravel 5.6

我使用 Laravel 5.6

I want change author_IDto user_id

我想改变author_IDuser_id

I have columns as mentioned bellow:

我有如下提到的列:

class CreatePostsTable extends Migration{

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->bigInteger('author_ID');
            $table->bigInteger('category_ID')->nullable();
            $table->longText('post_content');
            $table->text('post_title');
            $table->string('post_slug', 200);
            $table->string('post_type', 20);
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

I use the below link to change my column name :

我使用以下链接更改我的列名:

How can I rename column in laravel using migration?

如何使用迁移重命名 Laravel 中的列?

then create blow migration :

然后创建吹迁移:

php artisan make:migration rename_author_id_in_post_table

rename migration file :

重命名迁移文件:

class UpdateUsernameInPostTable extends Migration{

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->renameColumn('author_ID', 'user_id');
        });

    }

    public function down()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->renameColumn('user_id', 'author_ID');
        });
    }
}

but with run command : php artisan migratei have below error :

但是使用运行命令:php artisan migrate我有以下错误:

Symfony\Component\Debug\Exception\FatalThrowableError  : 
Class 'RenameAuthorIdInPostTable' not found

回答by AntoineB

The error comes from the fact that your class name doesn't correspond to the name of the PHP migration file. Filenames are used to determine the name of the class they contain, so it is important to not rename a class or a file without renaming the other.

该错误来自于您的类名与 PHP 迁移文件的名称不对应的事实。文件名用于确定它们包含的类的名称,因此在不重命名另一个类或文件的情况下不要重命名另一个类或文件是很重要的。

Rename your class UpdateUsernameInPostTableto RenameAuthorIdInPostTableand it should work.

将您的类重命名UpdateUsernameInPostTableRenameAuthorIdInPostTable它应该可以工作。

If it does not, run composer dumpautoto reload the autoloading file and it will definitely work.

如果没有,请运行composer dumpauto以重新加载自动加载文件,它肯定会起作用。

回答by Chirag Patel

Just try this code after deleting your existing migrationfor updating name,

只需在删除现有迁移以更新名称后尝试此代码,

php artisan make:migration rename_author_id_in_posts_table --table=posts

It will create one migration and then replace up() and down() functions with this in that created migration,

它将创建一个迁移,然后在创建的迁移中用这个替换 up() 和 down() 函数,

public function up()
{
    Schema::table('posts', function (Blueprint $table) {
        $table->renameColumn('author_ID', 'user_id');
    });
}

And down() function with this,

和 down() 函数,

public function down()
{
    Schema::table('posts', function (Blueprint $table) {
        $table->renameColumn('user_id', 'author_ID');
    });
}

For more information about migration, you can go through this link Laravel Migrations.

有关迁移的更多信息,您可以通过此链接Laravel Migrations

I hope this will work for you. If any doubts please comment.

我希望这对你有用。如有疑问请评论。

PROBLEM WITH YOUR MIGRATION:

您的迁移问题:

You are using Schema::create()method for change in table, As you have already created migration for posts table,You don't need to do Schema::create()just use Schema::table()method to update and change in any fields.

您正在使用Schema::create()表中的更改方法,因为您已经为帖子表创建了迁移,您不需要Schema::create()只使用Schema::table()方法来更新和更改任何字段。

Schema::create('posts', function (Blueprint $table) {
        $table->renameColumn('author_ID', 'user_id');
    });