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
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'); });
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');
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).
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');
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With