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

maxdiable's avatar

sanctum with multi connection

hi, i have 2 connection oracle_1 and oracle_2

i have created:

class PersonalAccessToken extends SanctumPersonalAccessToken { protected $connection = "oracle_1"; protected $table = 'personal_access_tokens';

//protected $guarded = ['name'];

}

in AppServiceProvider i put:

public function boot() { Sanctum::usePersonalAccessTokenModel( PersonalAccessToken::class ); }

my guards:

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

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

the login api working. after i call another route with postman and i use authentication Barear... but the route is not called.. class OperatoriController extends Controller { public function index(Request $request) :JsonResponse { response()->json(['op' => true, 'message' => $message]); } }

my api:

Route::middleware('auth:sanctum')->group(function () { Route::apiResource('operatori', OperatoriController::class); });

any help?

my api

0 likes
2 replies
tisuchi's avatar

@maxdiable It looks like you've set up your PersonalAccessToken model to use the "oracle_1" connection and specified it in your AppServiceProvider, but you're not specifying the connection in your API routes. To fix this, you can add the connection to your API routes by using the on method:

Route::middleware('auth:sanctum')->group(function () {
    Route::on('oracle_1')->apiResource('operatori', OperatoriController::class);
});

This tells Laravel to use the "oracle_1" connection when handling requests to the "operatori" routes.

You can also use on method on your personal access token model to use the different connection for a different tokens if you have different token tables on a different connection

class PersonalAccessToken extends SanctumPersonalAccessToken { 
    protected $connection = "oracle_1"; 
    protected $table = 'personal_access_tokens';
}
class OtherPersonalAccessToken extends SanctumPersonalAccessToken { 
    protected $connection = "oracle_2"; 
    protected $table = 'other_personal_access_tokens';
}
Sanctum::usePersonalAccessTokenModel( OtherPersonalAccessToken::class, 'other_connection' );

This way you can use the different connections for the different token tables.

maxdiable's avatar

Thanks for the reply, I'll try to follow your advice. I thought it was enough to indicate the connection in the model.

br Max

Please or to participate in this conversation.