Nertskull
Nertskull

Reputation: 493

Laravel 8 changing users table name in App/Model with protected not working

I have a relatively fresh install of laravel 8 with jetstream/fortify. I'm trying to use a pre-existing user table that has a different table name than the default 'users'.

From reading around I thought I had to just update that in the app\Model\User.php. Which I have done as follows:

 class User extends Authenticatable               
 14 {                                                                                                       
 15     /**                                                                                                                                                                                                
 16     .* My custom users table                        
 17     .*/
 18     protected $table = "user";  // note here it is user not users
 19     protected $primaryKey = "user_id";

But when I try to register a new user, I get SQLSTATE errors that Base table or view not found...."mydb.users" doesn't exist.

So it's still looking for 'users' instead of 'user'.

Where else do I need to change this?

The documentation doesn't seem to mention anywhere else, and other stack answers seem to suggest this should be enough

Edit: This is my config/auth.php portion

 67                                                                                         
 68     'providers' => [                                                                                 
 69     .   'users' => [                                               
 70     .   .   'driver' => 'eloquent',                                           
 71     .   .   'model' => App\Models\User::class,                                                                                  
 72     .   ],                                                                                                                                                                                                               
 73                    
 74     .   // 'users' => [                                               
 75     .   //     'driver' => 'database',                              
 76     .   //     'table' => 'users',                                               
 77     .   // ],                                                     
 78     ],     

So I believe I am using the right user provider with eloquent.

Upvotes: 2

Views: 2566

Answers (1)

N69S
N69S

Reputation: 17216

Validation rules for unicity of the email have the table name hardcoded in it (it happens before initialising any model so it makes sense in a way).

something that looks like this

$validator = Validator::make($request->all(), [
    'email' => 'email|unique:users', //<- notice here that the database table name is hardcoded
    'password' => 'required',
]);

If you're using default validator, then you need to replicate it and change that line.

Upvotes: 7

Related Questions