How to Change Table Name using Laravel Migration?

July 18, 2022
Laravel
How to Change Table Name using Laravel Migration?

In this tutorial, you will learn how to change the table name in the laravel migration.

There are many times when you need to change the table name, this may be due to a change in requirement or to further extend the system.

Thanks to the migration, you can change the table. In this tutorial, we will change the users table to the customers table.

Let’s get started!

Step 1: Install Laravel

First of all, we will install the laravel to the folder. I am installing it in the change_table_name folder:

composer create-project --prefer-dist laravel/laravel change_table_name

Step 2: Update DB Credentials

Then in the .env, change the DB_PORT, DB_DATABASE, DB_USERNAME and DB_PASSWORD according to your database details.

Step 3: Migrate Database

Then you need to migrate the database by using the following command:

php artisan migrate

After this step, you will see that all the tables including users will get migrated.

Step 4: Create New Migration for Changing Table Name

Now to change the users table to customers, we will create the new migration file by using the following command:

php artisan make:migration rename_users_table_to_customers

Whereas rename_users_table_to_customers is the name of the migration file, you can name it anything you want.

Step 5: Edit the Migration file

Now go to the database/migrations and open rename_users_table_to_customers migration file which we created, you will see something like this:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class RenameUsersTableToCustomers extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('customers', function (Blueprint $table) {
            //
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('customers', function (Blueprint $table) {
            //
        });
    }
}

Step 6: Rename the table

You can empty the up() function and add the following command into the up() function, like this:

 public function up()
    {
        Schema::rename('users', 'customers');
    }

This is to rename the table from users to customers.

Step 7: Adding foreign Keys and indexes (if you have any)

The data will remain as it is, but if you have foreign keys and indexes you will not able to delete them after renaming, so its best to first remove foreign keys and indexes, then rename it and then apply foreign keys and indexes again.

public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropForeign(['user_type']);
        });

        Schema::rename('users', 'customers');

        Schema::table('customers', function (Blueprint $table) {
            $table->foreign('user_type')->references('id')->on('user_types');
        });
        
    }

Step 8: Create a new model or add a table name to the existing model

Since the table name will be changed, now you either can have a new model Customer for the customers or you have added the table name in the User Model.

Option 1: How to make a new model for Customer

This is how you can make new model for the customers :

php artisan makel:model Customer

Option 2: How to add the table name in the User Model

You can add the table name in the User model:

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;
    protected $table="customers";

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

...

Step 9: Run the migrate command again

Now run the migrate command once again:

php artisan migrate

You will see that the users will be now renamed as the customers.

Conclusion:

Hope you have learned today in the above post how to change the table name in the laravel migration.

I will see you in the next one 🙂

Write a Reply or Comment

Your email address will not be published. Required fields are marked *


Icon