Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rename column in laravel using migration

I use laravel 5.6

I want to change author_ID to user_id

I have columns as mentioned below:

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

I use the below link to change my column name :

How can I rename a column in laravel using migration?

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 the run command : php artisan migrate I have the below error :

Symfony\Component\Debug\Exception\FatalThrowableError  : 
Class 'RenameAuthorIdInPostTable' not found
like image 428
Mostafa Norzade Avatar asked Jul 02 '18 06:07

Mostafa Norzade


People also ask

How do I rename a column in Laravel migration?

To rename a column, you may use the renameColumn method on the Schema builder. Before renaming a column, be sure to add the doctrine/dbal dependency to your composer. json file: Schema::table('users', function (Blueprint $table) { $table->renameColumn('from', 'to'); });

How do I rename a table in Laravel migration?

To change a table name, you can do this: Schema::rename($currentTableName, $newTableName); You can use the drop or dropIfExists methods to remove an existing table: Schema::drop('users'); Schema::dropIfExists('users');

How do I add a column to an existing table of Laravel in a migration?

The make:migration Artisan in Laravel (9) enables you to generate a database migration. You can find the resultant migration files are the database/migrations directory. Using this command, we can add a new column to a table (in addition to other operations).


2 Answers

Just try this code after deleting your existing migration for 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,

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

And down() function with this,

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.

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('posts', function (Blueprint $table) {
        $table->renameColumn('author_ID', 'user_id');
    });
like image 182
Chirag Patel Avatar answered Dec 02 '22 19:12

Chirag Patel


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.

Rename your class UpdateUsernameInPostTable to RenameAuthorIdInPostTable and it should work.

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

like image 39
AntoineB Avatar answered Dec 02 '22 20:12

AntoineB