How to rename users DB table in default Laravel Auth

Laravel framework comes with built-in Auth mechanism for logging-in and registering users. As a part of that we have starting migration files, one of them is creating a main Auth DB table called users. But what if we want to change that table name, and use Users for some other purpose? It's actually pretty easy to do. Database table name for Auth is configurable. Not perfectly easy, but there are four places you need to change: 1. Migration file After installing Laravel, you have this migration file 2014_10_12_000000_create_users_table.php:
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('users');
    }
So here you can change your table name to anywhere you want - as long as you do it in both up() and down() functions, like this:
    public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('admins');
    }
Then you run artisan migrate command and see the same text mentioning users migration:
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
But under the hood you know it will be admins. Or, in fact, you can rename files as well for the consistency. 2. Change the table in app/User.php In the main Eloquent model, which takes care of users, we can specify another table:
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
Change 'users' to 'admins', that's it. 3. Change the table in config/auth.php Same small change in the config - from 'users' to 'admins':
    /*
    |--------------------------------------------------------------------------
    | Authentication Table
    |--------------------------------------------------------------------------
    |
    | When using the "Database" authentication driver, we need to know which
    | table should be used to retrieve your users. We have chosen a basic
    | default value but you may easily change it to any table you like.
    |
    */

    'table' => 'users',
4. Validation rule - unique email check In default app/Http/Controllers/Auth/AuthController.php there are validation rules listed. One of them states that email has to be unique - so we need to change the table it is checking:
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }
Change the part 'unique:users' to 'unique:admins'. * * * * * And that's it - from now, whatever Auth::xxxxx() function you call - it will work with your admins table.

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 58 courses (1054 lessons, total 46 h 42 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials