Support the ongoing development of Laravel.io →
Authentication Sanctum Database

Hi,

I'm trying to build an API for already existing PHP system which uses mysql database served on different server, let's call it: DB_API.

I also need to protect it using laravel:sanctum in order to manage tokens and authenticate requests, but my problem is that I need all sanctum actions (generating tokens and authenticating requests) to be done on separate database, let's call it: DB_AUTH.

What I did so far is:

  • define two DB connections in the .env and database.php files,
  • define the $connection attribute in the User model to use the DB_AUTH database connection,
  • protect the API route of an end-point with the ['auth:sanctum'] middleware,

The result is when calling the protected API endpoint it's not using the DB_AUTH connection, but the DB_API connection which failed to find the personal_access_tokens table and that is correct cause this table isn't defined in the this database.

Any guidance is appreciated,

Best regards,

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Naaman naamaan Joined 16 Feb 2025

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.