rename column in laravel using migration

Multi tool use
rename column in laravel using migration
I use laravel 5.6
I want change author_ID
to user_id
author_ID
user_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?
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 migrate
i have below error :
php artisan migrate
SymfonyComponentDebugExceptionFatalThrowableError :
Class 'RenameAuthorIdInPostTable' not found
2 Answers
2
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()
Schema::create()
Schema::table()
Schema::create('posts', function (Blueprint $table) {
$table->renameColumn('author_ID', 'user_id');
});
create
Great @MostafaNorzade
– Chirag Patel
Jul 2 at 7:36
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.
UpdateUsernameInPostTable
RenameAuthorIdInPostTable
If it does not, run composer dumpauto
to reload the autoloading file and it will definitely work.
composer dumpauto
Thanks . i changed my class name and run
composer dumpauto
. run migrate
agin . i have blow error: SymfonyComponentDebugExceptionFatalThrowableError : Class DoctrineDBALDriverPDOMySqlDriver' not found
– Mostafa Norzade
Jul 2 at 7:04
composer dumpauto
migrate
SymfonyComponentDebugExceptionFatalThrowableError : Class DoctrineDBALDriverPDOMySqlDriver' not found
You have to add the following package:
doctrine/dbal
, run the command composer require doctrine/dbal
, and then php artisan migrate
, it should work.– AntoineB
Jul 2 at 7:11
doctrine/dbal
composer require doctrine/dbal
php artisan migrate
Thanks . i added
doctrine/dbal
package and i have new error : IlluminateDatabaseQueryException : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') default character set utf8mb4 collate 'utf8mb4_unicode_ci'' at line 1 (SQL: create table 'posts' () default character set utf8mb4 collate 'utf8mb4_unicode_ci')
– Mostafa Norzade
Jul 2 at 7:23
doctrine/dbal
IlluminateDatabaseQueryException : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') default character set utf8mb4 collate 'utf8mb4_unicode_ci'' at line 1 (SQL: create table 'posts' () default character set utf8mb4 collate 'utf8mb4_unicode_ci')
Class names have not issue here. Just need to use
Schema::table()
will solve the error. @AntoineB– Chirag Patel
Jul 2 at 7:35
Schema::table()
@ChiragPatel Yes indeed you are right, but I'm fairly certain that the class name still has to match the name of the file, so issue was coming from both :)
– AntoineB
Jul 2 at 7:42
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks . Yeeeees i used
create
....– Mostafa Norzade
Jul 2 at 7:35