I have seen an option to change the database table name from users to something else, but the model is still User in the code. For consistency I am looking to know how I can change the name of the model to something else. In my case I want to call it "Customer" with the table name of "customers".
Asked
Active
Viewed 4,923 times
1
-
Just change the classname, for consistency you may also change the filename. – Leyiang Jun 29 '20 at 06:05
-
The problem is the auth logic looks for the model named User, which is probably why all the articles about changing this stop at merely changing the table name because that's rather trivial. I can't begin to know everywhere where there is a reference to the "User" model that I need to change, hence the question... – Jun 29 '20 at 06:18
-
You may try to change the auth.php file and change the model name to whatever you renamed the User Model – Desmond_ Jun 29 '20 at 06:59
-
You will find the answer here. [stackoverflow.com: How to change the user model in laravel to use other table for authentication than users?](https://stackoverflow.com/questions/41014735/how-to-change-the-user-model-in-laravel-to-use-other-table-for-authentication-th) – Kongulov Jun 29 '20 at 07:02
-
P.S. if you create a customer model the table name will be automatically customers. – jewishmoses Jun 29 '20 at 07:17
-
@jewishmoses yes that would...create a random model that is in no way connected to the authentication mechanisms in laravel...clearly NOT what I was asking for if you'd read.... – Jun 29 '20 at 07:43
-
1@RamizKongulov now there's an answer that actually addresses what I asked for. Thanks for actually reading the question unlike others...:) – Jun 29 '20 at 07:45
-
@user738974I was glad to help =) – Kongulov Jun 29 '20 at 07:51
1 Answers
3
The User model is declared in the file: config/auth.php
...
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
...
To change your user model you can change the file name or make a new model and then change the declaration in config/auth.php
...
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Customer::class,
],
...
For consistent models and migration in you can generate them both with a single artisan command:
php artisan make:model Customer --migration
This will generate a singular instance for the model name and plural for the table name.

jeremykenedy
- 4,150
- 1
- 17
- 25
-
One thing you did miss is the use statement at the top of the RegisterController, it calls the User model so that needed to be renamed as well. After changing that it works. – Jun 29 '20 at 11:08