rename column in laravel using migration
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.
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');
});
Laravel's rename column migration altering the position of column
You can not do this,
$table->renameColumn('col_old_name', 'col_new_name')
->after('col_to_be_after')
renameColumn()
returns Illuminate\Support\Fluent
but ->after()
is under Illuminate\Database\Schema\ColumnDefinition
. So that's why you code does not work.
Without losing the data you may have to:
create a new one in a specific position :
$table->string('xyx')->after('qwe');
copy the data from the old one to the new one.
delete the old col.
Laravel: Rename table field without deleting data during migration
For that you have to follow below steps!
1. install Doctrine/dbal
composer require doctrine/dbal
2. create a migration file to rename column name
php artisan make:migration updateTableColumnName
3. add below code to edit your column name
Schema::table('YourTableName', function (Blueprint $table) {
$table->renameColumn('name', 'username');
});
And Run the migration you have done it without losing your data.
php artisan migrate
Laravel Migration: Rename and Add column with similar name ends up with error
As you suspected the issue might be in Laravel migration. But you can still use the Laravel migration to alter the table, just separate the execution as they are not running in a way that you are expecting. We're modifying only the down()
method, as where the issue actually is.
Method 1
public function down()
{
Schema::table('extensions', function (Blueprint $table) {
$table->renameColumn('extended_amount', 'extended_amount_final');
});
Schema::table('extensions', function (Blueprint $table) {
$table->decimal('extended_amount', 22, 2)->nullable();
});
}
Method 2
You can use the DB
Facade to do this by raw SQL in a separate query.
use Illuminate\Support\Facades\DB;
public function down()
{
Schema::table('extensions', function (Blueprint $table) {
$table->renameColumn('extended_amount', 'extended_amount_final');
// $table->decimal('extended_amount', 22, 2)->nullable();
});
DB::statement("ALTER TABLE project_extensions ADD COLUMN extended_amount DECIMAL(22,2) NOT NULL;
}
Thanks to my colleagues: Ms. Mowshana Farhana and Mr. Nazmul Hasan, for assisting in debugging the issue and digging up a solution.
Error when renaming Table Column in Laravel migration
You have to wrap your column names with dash to quotes, because generated SQL tries to use it like a minus sign
e.g. $table->renameColumn("`name-ar`", "`name_ar`");
Renaming column in Laravel migration, forcing lowercase
What I ended up doing because I wont spend too long trying to resolve this issue is using a raw statement.
public function up()
{
DB::statement('alter table `Table-Name` change column `Column_Name_ID` `Column_Name` integer(11) null');
}
/**
* Reverse the migrations.
*
* @return void
*/
///
Laravel migrate failing on renameColumn when running
It seems to be solved in v2.7 [bug] Don't skip column options. #3089.
2.7 breaks renaming columns in mysql 5.7 #3091
Upgrade the package version using composer update doctrine/dbal
.
Laravel Migration - Data type also changes when renaming column
I learned that there is an issue in Schema regarding the enum and still in discussion until now.
I just used the other way to update the datatype of the column.
DB::statement('ALTER TABLE reservations MODIFY COLUMN payment_amount_full double(10,2)');
and this works for me. If there are other ways on how to do this, please let me know. Thanks.
Related Topics
Get String Between - Find All Occurrences PHP
Setting Element of Array from Twig
How to Connect User with a Login Cookie in PHP
PHP - Get the Size of a Directory
How to Detect and Handle MySQL Warnings with PHP
Permission Denied Despite Appropriate Permissions Using PHP
Checking for Empty Arrays: Count VS Empty
Convert the First Element of an Array to a String in PHP
Creating Variable Name by Concatenating Strings in PHP
How to Print the Actual Query That MySQLi->Execute() Makes
PHP How to Go One Level Up on Dirname(_File_)
PHP Date Showing '1970-01-01 ' After Conversion
How to Write Laravel Groupby Query
Fatal Error: Cannot Use Object of Type Stdclass as Array In
Understanding Doctrine Cascade Operations
Rename an Uploaded File with PHP But Keep the Extension