Get current personal access token in Laravel sanctum from request

Get current personal access token in Laravel sanctum from request

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Retrieving Personal Access Tokens in Laravel Sanctum v3

When building an API with Laravel Sanctum, you often encounter a specific architectural challenge: the client authenticates using a pre-shared token, but your server-side logic needs to reference that same token value. A common scenario involves copying a token from the response headers or storage and then attempting to retrieve its properties within a subsequent controller request.

In Laravel Sanctum v3, accessing this data requires understanding how the auth:sanctum middleware interacts with the PersonalAccessToken model. The most intuitive approach for many developers is to attempt direct access via the authenticated user's relationship:

return response()->json([
    'token' => Auth::user()->token
], 200);

However, this code block frequently triggers the error: The attribute [token] either does not exist or was not retrieved for model. This isn't a bug in Sanctum; it is a result of how Eager Loading and Relationship Accessors work by default. By default, Laravel does not automatically eager load relationships unless you explicitly tell it to do so. The token relationship on the User model exists, but without with('token'), the underlying query returns a user where that specific relationship column is null or inaccessible via the accessor in this context.

The alternative approach often suggested is querying the token table directly using the user's ID:

PersonalAccessToken::find(Auth::id());

This returns null because Auth::id() retrieves the ID of the currently authenticated user, not the ID of the specific Personal Access Token. In Sanctum, the relationship between a user and their token is not a simple one-to-one mapping based on the user's primary key. Instead, tokens are linked to users via the tokenable_id column, which stores the foreign key, and tokenable_type, which specifies whether that key belongs to a User or another model like an API Token.

To successfully retrieve the token string from within the controller when you already know the user is authenticated, you must leverage the auth:sanctum middleware's available helper or explicitly eager load the relationship. The most robust solution involves accessing the token through the authenticated request instance, which Sanctum populates specifically for API requests.

You can access the current token via the $request object if you are inside a controller method protected by the auth:sanctum middleware:

public function show(Request $request)
{
    // The 'auth:sanctum' middleware ensures $request->user() is loaded with the token relationship
    // However, explicit eager loading is often safer to avoid N+1 issues or null checks.
    
    $token = $request->user()->token;

    if (! $token) {
        abort(401, 'No personal access token found.');
    }

    return response()->json([
        'token' => $token->plain_text // Use plain_text to get the raw string without hash prefix
    ]);
}

Note that accessing plain_text is crucial here. Sanctum stores tokens in a hashed format by default for security reasons. The plain_text attribute decrypts this value so you can return the actual token string that matches the one the client used to authenticate. If you simply access $token->plain_text and it returns null, ensure that the token was created with hashing disabled or that you are accessing the correct relationship instance.

Another valid pattern is using auth:sanctum's built-in user() helper combined with an explicit eager load:

use Illuminate\Support\Facades\Auth;

public function show()
{
    $user = Auth::user();
    
    // Explicitly eager loading the token relationship ensures data availability
    $user->load('token'); 

    return response()->json([
        'token' => $user->token?->plain_text
    ]);
}

This approach decouples the retrieval from the specific request instance, making it useful if you need to pass the user object around before responding. However, be cautious about exposing tokens in responses unless they are intended for immediate client-side use or token rotation logic.

If your goal is strictly to echo back the token used in the current request session (for debugging or refresh workflows), ensure that the token was indeed created with plain_text enabled or that you are correctly accessing the decrypted attribute. Always validate the existence of the token before attempting to access its properties to prevent runtime exceptions in production environments.

Our network