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.

Installing Laravel using the Laravel Installer

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:

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAdministratorsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('administrators', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('administrators');
}
}

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.

Laravel migrations

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:

<?php
namespace ZtuX;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Administrator extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}

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.

Home Page

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

php artisan:make auth
Trying 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”.

Using the Administrator model.

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

/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:administrators',
'password' => 'required|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return Administrator
*/
protected function create(array $data)
{
return Administrator::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}

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!

Home page after login.

Questions?

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

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.