user18630923
user18630923

Reputation:

How to change the name of 'name' column in the 'users' table?

I have a Laravel app with the users table. I want it (the table) to have username column instead of name column, i.e. structure like this:

|  id |  username  |        email       |    password   | ...etc. |
| --- | ---------- | ------------------ | ------------- | ------- |
|  1  | Username 1 | [email protected] | xEvehgv#52... |   ...   |
|  2  | Username 2 | [email protected] | dkjbg#%R!D... |   ...   |

How can I do this with the help of Laravel 9 without having problems with form data validation and third-party packages? Thank you in advance!

Upvotes: 0

Views: 333

Answers (2)

ERaufi
ERaufi

Reputation: 1663

Note: changing the column name in the users table is not a good idea because a lot of packages use the default name of the table. Instead, you can add a new column with your customer name.

For example:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('username');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

Otherwise, you can achieve this by going to database/migrations/_create_users_table.php

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('username');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

And also you have to run migrations

php artisan migrate

or execute this command:

php artisan migrate:refresh

Upvotes: 0

user18630923
user18630923

Reputation:

I've just found the answer to my question here. Thank you, everyone, for paying attention to this question.

Upvotes: 0

Related Questions