Hello artisan, hope you are doing a great job. In this quick example i will show you how you can change you datatabe table column name using migration command. To create this change mysql column name in laravel, i will using this doctrine/dbal
composer package.
This laravel migration change column name example, i will show you the example code with migration command. Sometimes we need to change our mysql column name using migration. That's why i am here to help you.
To do laravel change column name, install this composer package by the following command:
composer require doctrine/dbal
Now assume we have a students
table and there is field name fullname
. Now we would like to change it fullname
to username
. Let's do that.
php artisan make:migration rename_fullname_in_students_table --table=students
Now update your newly created migration file like below.
database/rename_fullname_in_students_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class RenameFullnameInStudentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('students', function (Blueprint $table) {
$table->renameColumn('fullname', 'username');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('students', function (Blueprint $table) {
$table->renameColumn('fullname', 'username');
});
}
}
Execute command to change or rename the column value fullname
to username
:
Read also: Rollback Your Database Migration in Laravel
Hope it can help you.
#laravel #laravel-8x