Laravel 5.6 Auth to different tables in DB

Laravel 5.6 uses Auth with its database table - 'users' by default. I need to create 2 different users - simple user and user admin, so each of them will use there own Auth method and connect to there own database table.

For example: Auth::user(); - with table 'users' and i need to create another auth, like Auth::admin(); - with table 'admin'

How i can do that? Maybe copy and create another Auth?

(I am using laravel's api and passport).

I don't need to create new fields in the users table. I need to create a new table.

Upvotes: 0

Views: 820

Answers (1)

rayrayray9151
rayrayray9151

Reputation: 77

You have to create a middleware for admin authentication. Do so by typing this:

php artisan make:middleware Admin

This creates a middleware file called Admin.php

Register the middleware on Kernel.php

'admin' => \App\Http\Middleware\Admin::class

Add the admin middleware to a route in route.php file

get('protected', ['middleware' => ['auth', 'admin'], function() {
    return "this page requires that you be logged in and an Admin";
}]);

Add the isAdmin() method to User model for checking if it is admin or not.

public function isAdmin()
{
    return $this->admin; // this looks for an admin column in your users table
}

Upvotes: 1

Related Questions