Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lgendary's avatar

Multi auth on laravel using the same login page

I'm working on a project where I have Admin and Employee, I want the two of this user to login using one login form provided by laravel. This two users have a separate table.

This is the guard I define;

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],

    'employee' => [
        'driver' => 'session',
        'provider' => 'employees',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],
],


 'providers' => [
    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],

    'employees' => [
        'driver' => 'eloquent',
        'model' => App\Employee::class,
    ],

],

And I Overwrite attemptLogin in LoginController

protected function attemptLogin(Request $request){
$customerAttempt = Auth::guard('employee')->attempt(
    $this->credentials($request), $request->has('remember')
);
if(!$customerAttempt){
    return Auth::guard()->attempt(
        $this->credentials($request), $request->has('remember')
    );
} 
return $customerAttempt;


}

But is not working...I can't login with the employee user, it always return null when I dd(Auth::user());

0 likes
8 replies
jlrdw's avatar

Why not just have one table and use roles.

Or if using two tables you have to pull the employee from employee table.

bobbybouwmann's avatar

@jlrdw That's not his question right...?

@lgendary Are you sure the employee exists in the database with the given credentials? You can test it like this as an example in your route file:

Route::get('test-employee-login', function () {
    $credentials = [
        'email' => '[email protected]',
        'password' => 'pass1234',
    ];

    dd(Auth::guard('employee')->attempt($credentials), Auth::check());
});
jlrdw's avatar

I'm working on a project where I have Admin and Employee, I want the two of this user to login using one login form provided by laravel. This two users have a separate table.

This is the guard I define;

And I Overwrite attemptLogin in LoginController

But is not working...I can't login with the employee user, it always return null when I dd(Auth::user());

Because you said employee is another table.

I don't see actual question. And most answers on forum state:

a user is a user.

And talk about a single users table.

So ??

But if using two tables, just have separate areas, forms, and controller methods, etc:

Similar to:

Did I mis-understand something, are you just using one table.

Lgendary's avatar

@JLRDW - I pull employee from his own table but I can't make him to login using the same login page

jlrdw's avatar

I would search for other post here on authentication and authorization and highly recommend to just use one users table and implement roles.

To search go to Google and type in

site:laracasts.com authentication and authorization

Exactly like that.

The reason to search that way is Jeffrey's search here is not as powerful as a Google search.

You could also put in other buzz words like authorization and roles.

Right now authentication is looking at the users table for the data, is what I'm thinking.

site:laracasts.com authentication and authorization two tables

Brings up some pretty good results.

1 like
Lgendary's avatar

@JLRDW - All the resources are good and they also point to single table for user.... I believe they have experience more than me and I should follow the same process. But if there is a way to use two tables, I will definitely love to learn. Thanks guyz

jlrdw's avatar

You on the right track I just don't think you set up everything correctly see this

https://stackoverflow.com/questions/46292391/authenticate-users-from-more-than-two-tables-in-laravel-5

Do some more General searches I have seen articles on two or more tables before. But you have to set up everything just right.

Sometimes it's just easier to write your own custom RBAC system.

To me the basics of such a system is can a user with their logged in role do a certain method or not do a certain method.

Or put another way does their role match the required role. If a match they can if it does not match they cannot.

And the second part of course is a user being able to edit their own data only.

Which is easily done via the authenticated users ID.

Please or to participate in this conversation.