How to change your Authentication model in Laravel 5.3

Sometimes we need to change the default User model in our Laravel Application. So let’s get started with this tutorial.

First, we need to create a simple Laravel Application using the installer with composer. We can do it easily using the “laravel new auth_model_example” command.

Once our application is ready, we’ll open the current project using our favorite editor, in this case I’ll use Atom editor, you can get it here.

Changing the Users migration:

Go ahead and open the Users migration in auth_model_example/database/migrations/2014_10_12_000000_create_users_table.php file and we’ll rename it to “2014_10_12_000000_create_administrators_table.php”, for example.

Rename all the “Users” words to “Administrators” (or whatever that you want). So we get something like this:

Remember that you can add or delete fields that you need.

We are ready to run our migrations with the “php artisan migrate” command, and voilà, we have our custom table created.

Creating the custom model.

In this part you can use the User model and rename it, or delete it an create a new model using the “php artisan make:model [YourModel]”. We’ll use the User model and rename it to “Administrator”, so we get it something like this:

Also, rename it from “User.php” to “Administrator.php”. At this moment we can open our project in the “http://localhost:8000” url. Don’t forget to run your php server with the “php artisan serve” command. You can see the home’s Laravel project page.

Now, let’s create the Auth Controllers using the “php artisan make:auth” command and try to register an Administrator…

And… we get an error!

But, why? Because we need to change the Auth config file (you can find it in config/auth.php), and change the users provider model from “User::class” to “YourNewModel::class”, in this case “Administrator::class”.

Also, in our RegisterController we need to change the “validator” and “create” functions, like this:

Notice that we use our Administrator model, so we can add it with the “use” statement, and in the validator change from “users” to “administrators” (o whatever that you want in the “email” field).

We try to register a new Administrator again, and it works!

Questions?

You can find the application example in this repository on github.

Software Engineer with experience in mobile development using mainly agile methodologies. Experience with all Apple ecosystem.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store