COACHTHEM's avatar

How to rename default "users" table to something else in laravel 5.5

I want to rename the "users" table to "user". If I want to achieve that then what changes will I have to do in laravel 5.5

0 likes
8 replies
tykus's avatar
tykus
Best Answer
Level 104

In your User model, you need to set a protected $table = 'user'; property. I think that should do it.

1 like
COACHTHEM's avatar

Thanks @tykus . so with this I assume API and web guards will work fine. Also should I modify the migration file for users

tykus's avatar

I assume API and web guards will work fine

The model used rather than the query builder so you're okay.

should I modify the migration file for users

yes, of course

COACHTHEM's avatar

After adding protected $table = 'user'; line of code in User model and renaming "users" table to "user" I am not able to do SignUp getting below error.

 Illuminate \ Database \ QueryException (42S02)
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'lara6.users' doesn't exist (SQL: select count(*) as aggregate from `users` where `email` = [email protected])
tykus's avatar

Looks like an unique validation rule query. In the app/Http/Controllers/Auth/RegisterController.php has this rule. Change the table name in the rule:

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:user', // here
            'password' => 'required|string|min:6|confirmed',
        ]);
    }
3 likes
COACHTHEM's avatar

ok. I figured this out. I had to modify validation method in the Controllers/Auth/RegisterController.php

below is the code

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:user',
            'password' => 'required|string|min:6|confirmed',
        ]);
    }

But Still I wonder where else will I have to modify the code. It would have been great if I had to configure it at just one place.

tykus's avatar

The configuration is the $table property in the model. Validation is a slightly quirky side issue in that some rules do not rely on the Model, but rather on the table directly.

Please or to participate in this conversation.