Creating Our Admin Model and Admin Guard for Our Multiple Authentication System in Laravel 5.4

Welcome back to part II of this series where we will be creating multiple authentication system using Laravel 5.4. If you are just joining us, be sure to read part I of this tutorial. We have a lot to do, so let’s get started with part II.

If you recall in (part I: Creating Laravel Multiple Authentication System Using Default Authentication System in Laravel 5.4 ) , we used php artisan make:auth to create user authentication scaffold. This command also created the user model. Usually, Laravel store models in the app folder. Let’s have a look at the user model by going to app/User.php

Laravel is an MVC framework where MVC stand for: Model, View, Controller. To talk to our database we need models. User.php model has three fields which are fillable. To create our Admin.php model, we are going to duplicate User.php model.

Our Admin.php model should have the following code:

If you take a closer look at the line where we have: use Illuminate\Foundation\Auth\User as Authenticatable; This is our base model for Authenticatable users, it is usually located inside vendor. On class Admin extends Authenticatable, our Admin.php model extends Authenticatable which give us ability to use default Laravel authentication system.

We have our fillable array which matches what we have in our admin migrations. Fillable array give us the ability to mass assign values. We also have our hidden array. Hidden array gives ability to hide values from the collection of array.

That’s it, we have a table to store admins, we have a model that manages admins, now what we need is to set up our guard and we are done.

Creating Admin Guard

To configure our authentication settings, we need to locate auth.php which is located in config folder. The path to this file is config/auth.php, let’s open this file and have a look. In this file you will find four types of array namely: defaults, guards, providers and passwords.

What most people don’t understand is, Laravel has multiple ways to log people in and by default Laravel uses web guard to log in users. To create our admin guard, locate the guard array in auth.php and make sure it looks like the following code:

In the guard array we have created our admin guard but if you take a closer look at ‘providers’ => ‘admins’, we have provider which points to admins. Still in auth.php locate the providers array and make sure it has the following code:

When you look in providers array we have our admins provider. When you look at line which has ‘model’=>App\Admin::class, that is where we tie our guard with Admin.php model. Simply put, we are telling our admin guard to use Admin.php model to manage all the admins.

Look at line which has ”driver’ => ‘eloquent’, we are using eloquent driver to talk to our database and unless you are doing some custom query, we recommend using eloquent. Of course, if you are using database driver, you have to specify the table in the database instead of using a model. Look at commented code of users example on how to use database driver.

In auth.php, locate passwords array and make sure the array has the following code:

When we created our migration in part I, Laravel created password_resets table which we can use across multiple guards for password reset in case users forget their password. What we have done is, we have added admins in this array.

This will helps us in our last tutorial when we will be working with password resetting. Now make sure your auth.php has the following code:

 

The last thing we need to do is to tell Admin.php model to use admin guard. Our Admin.php should have the following code:

Look at line which has the following code protected $guard = ‘admin’; this is where we tell our Admin.php model to use admin guard. In the next tutorial, we will start by creating admins route and testing our guard. To get the code for this tutorial visit Laravel 5.4 Multi Authentication System

Facebook Comments
Get free email updates!
Signup now and receive an email once we publish new content.
We respect your privacy

Henry Mbugua

I am a Junior Developer ready to enter major leagues. My professional ambition is to stay versatile and be able to fill different roles within a product group. As such, I have been learning technology across frontend and backend.