使用迁移重命名laravel中的列[duplicate]

gcmastyq  于 2022-12-14  发布在  其他
关注(0)|答案(5)|浏览(134)

此问题在此处已有答案

Laravel migrations: Class "not found"(14个答案)
7天前关闭。
我用laravel 5.6
我要将author_ID更改为user_id
我的专栏如下:

class CreatePostsTable extends Migration{

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->bigInteger('author_ID');
            $table->text('title');
            $table->timestamps();
        });
    }

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

我使用下面的链接更改我的列名:
How can I rename a column in laravel using migration?
然后创建鼓风机迁移:

php artisan make:migration rename_author_id_in_post_table

重命名迁移文件:

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

但使用run命令:php artisan migrate我遇到以下错误:

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

brjng4g31#

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

php artisan make:migration rename_author_id_in_posts_table --table=posts

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

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

和down()函数,

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

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

迁移问题:

您正在使用Schema::create()方法更改表,因为您已经为posts表创建了迁移,您无需执行Schema::create(),只需使用Schema::table()方法更新和更改任何字段。

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

b91juud32#

这个错误是由于类名与PHP迁移文件的名称不对应。文件名用于确定它们所包含的类的名称,因此在重命名一个类或一个文件之前,不要重命名另一个类或文件,这一点很重要。
将您的类UpdateUsernameInPostTable重命名为RenameAuthorIdInPostTable,它应该可以工作。
如果没有,运行composer dumpauto重新加载自动加载文件,它肯定会工作。

dz6r00yl

dz6r00yl3#

通过以下步骤,我可以在Laravel 8中重命名列名

**步骤1:**输入以下命令以创建新的迁移文件

php artisan make:migration update_author_ID_column --table=posts

**第二步:**在创建的文件中写入以下代码。按照以下代码执行up()函数

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

**步骤3:**在down()函数中写入以下代码

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

**步骤4:**在运行迁移命令之前,请安装doctrine/dbal依赖项

composer require doctrine/dbal

**步骤5:**最后运行迁移命令。

php artisan migrate
fnx2tebb

fnx2tebb4#

如果不想使用教条,可以使用DB语句,

public function up()
{
    DB::statement('ALTER TABLE posts CHANGE author_ID user_id bigint(20) ');
}
r1wp621o

r1wp621o5#

最好的方法是检查我们的表中是否有一些列,只是在迁移时避免错误;)

public function up()
    {
        if (Schema::hasColumn('table_name', 'old_col_name'))
        {
            Schema::table('table_name', function (Blueprint $table)
            {
                $table->renameColumn('old_col_name', 'new_col_name');
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        if (Schema::hasColumn('table_name', 'new_col_name'))
        {
            Schema::table('table_name', function (Blueprint $table)
            {
                $table->renameColumn('new_col_name', 'old_col_name');
            });
        }
    }

相关问题