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

tsitsi98's avatar

Log in with two different models

I have two models that i want to authenticate with and log in. The user model and the stations model! I wrote this code here located in JestreamServiceProvider:

 Fortify::authenticateUsing(function (Request $request) {
            $user = User::where('email', $request->identifier)->first();
            $station = Station::where('username', $request->identifier)->first();

            if ($user && Hash::check($request->password, $user->password)) {
                return $user;
            }

            if ($station && Hash::check($request->password, $station->password)) {
                return $station;
            }

            return null;
        });

Both users and stations log in from the same login page. i successfully authenticate using the stations username and password but it logs me in as a user! Its like it takes the id from the station and proceeds to log in with user using the stations id for users id or something like that. Can anyone help me how to use two different models to authenticate and log in? i already have set up in the config/auth.php guards and providers for users and stations like this:

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'stations' => [
            'driver' => 'session',
            'provider' => 'stations',
        ],
],
'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

        'stations' => [
            'driver' => 'eloquent',
            'model' => App\Models\Station::class,
        ],
    ],

0 likes
11 replies
Snapey's avatar

Guards are always problematic and should be avoided.

tsitsi98's avatar

@Snapey what should be then the best solution to implement the two model authentication/log in feature?

tsitsi98's avatar

@martinbean Stations are related to the user, one to many! I use roles but they are attached only to users

martinbean's avatar

@tsitsi98 Exactly. A user. So just let the user log in. If they have multiple stations and need to work with one at a time, then show a station select list or something that then redirects them.

tsitsi98's avatar

@martinbean I see your point, but in my case it is not applicable! I need direct access to the web interface from the stations model. The alternative is to move the station model as a user and assign a role which is something that I wish to avoid since it requires changes to a lot of controllers and relations. Is there anything else that I can do to manage the authentication proccess?

martinbean's avatar

@tsitsi98 Why do you need to log in as a “station” if a station is actually related to a user?

tsitsi98's avatar

@martinbean Think of it as the user having the stations is an admin for the stations and the stations being the end users. Each station has its own settings (user can edit those as well) but the station can edit only its own settings. The redirection you stated above is implemented but I'm trying to authenticate the station to no avail.

martinbean's avatar

@tsitsi98 You’re completely over-complicating things.

Take Shopify. You can have one shop, or multiple shops. Does Shopify have some weird login system where you can log in as a user, or as a shop? No. You log in with your user account, but then choose which ship you want to manage if you belong to multiple.

Follow this approach. You have users. Users have stations. You log in as a user to manage stations.

1 like
JussiMannisto's avatar

I made a SaaS platform with Laravel 5 about 8 years ago. I decided to make separate models for users and admins because it seemed safer or something. I maintain the app to this day and it has grown much bigger.

If I had a time machine, I wouldn't go back to kill Hitler. I'd go back 8 years and slap myself on the head.

One of these days I'll merge the user bases. But it's really painful now because of how complex the database schema has become.

Don't do dumb stuff like this.

2 likes

Please or to participate in this conversation.