skip to Main Content

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!

2

Answers


  1. Chosen as BEST ANSWER

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


  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search