Skip to content
Advertisement

Laravel 7 sanctum Database Connection Problem

I am making project for API by using Sanctum laravel package. It needs to connect 2 database servers. One is MySQL to save some data including login user. Another one is SQL Server to get data for API route. There is no problem in login for Sanctum in MySQL database server that I have tested login well and got back token. Also retrieving data from SQL Server is working well when I retrieve data without using Laravel Sanctum.

I need to login check from MySQL database server and retrieve data from SQL Server database server. The problem I am facing now is how and where to define MySQL database server need for Sanctum auth middleware.

The connections are as below:

JavaScript

For that route

JavaScript

I run it with auth token after logging success, I got the error as below:

IlluminateDatabaseQueryException: SQLSTATE[42S02]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid object name ‘personal_access_tokens’. (SQL: select top 1 * from [personal_access_tokens] where [token] = bd1de1df8bc2ac289dbd89f7d640a4c65ebc1b709b565ee500315ee710f12b62) in file …vendorlaravelframeworksrcIlluminateDatabaseConnection.php on line 671

personal_access_tokens and users tables are in the MySQL database. Can I customize which db connection is for Sanctum Auth? and how?

Advertisement

Answer

well you need some customization for personal access token. create a normal model with name of SanctumPersonalAccessClient(what ever you want its in this example) and in AuthServiceProvider.php write this

JavaScript

then in your model that just created just set connection and table name like this:

JavaScript

helpful link but with passport: https://github.com/laravel/passport/issues/247#issuecomment-424095961

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement