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

HadyElMaageny's avatar

$user = $request->user(); fetch the default guard from Database 'api'

I have two authentication setups in my Laravel application:

Admins: Model: Admin Table: admins Guard: admin-api

Users: Model: User Table: stakeholder_users Guard: api

When an admin logs in, the oauth_access_tokens table correctly stores the user_id of the Admin. However, when I call $user = $request->user();, it retrieves the user from the User model (stakeholder_users table) using the api guard instead of the admin from the admins table using the admin-api guard.

How can I ensure $request->user() correctly retrieves the authenticated admin when using the admin-api guard?

0 likes
3 replies
jayandholariya's avatar

@hadyelmaageny You can try this

$admin = Auth::guard('admin-api')->user(); // For Admin

$user = Auth::guard('api')->user(); // For User

Please or to participate in this conversation.