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.

Like our articles?
Check out our Laravel online courses!

8 COMMENTS

  1. Thanks, it really helps. i didn’t revise variable ‘table’ in config\auth.php and validator function in app/Http/Controllers/Auth/AuthController.php. Makes me really headache. Now it works thanks!

  2. Hi, i tried your solution – it looks all alright but it ends up with error message for me:
    SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ” for key ’email’ (SQL: insert into `User` (`updated_at`, `created_at`) values (2016-04-29 14:29:04, 2016-04-29 14:29:04))

    did you came across with simillar problem?

  3. QueryException in Connection.php line 620:
    SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘my-project-laravel.users’ doesn’t exist (SQL: select count(*) as aggregate from `users` where `email` = cristi@yahoo.com) .. help

LEAVE A REPLY

Please enter your comment!
Please enter your name here