Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

470
Visualizações
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:

DB_MYSQL_CONNECTION=mysql
DB_MYSQL_HOST=127.0.0.1
DB_MYSQL_PORT=3306
DB_MYSQL_DATABASE=api
DB_MYSQL_USERNAME=root
DB_MYSQL_PASSWORD=

DB_SQLSRV_CONNECTION=sqlsrv
DB_SQLSRV_HOST=127.0.0.1
DB_SQLSRV_PORT=1433
DB_SQLSRV_DATABASE=StoreData
DB_SQLSRV_USERNAME=sa
DB_SQLSRV_PASSWORD=

For that route

Route::middleware(['auth:sanctum'])->group(function () {  
    Route::get('/items', 'APIController@getAllItems');
});

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

Illuminate\Database\QueryException: 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 ...\vendor\laravel\framework\src\Illuminate\Database\Connection.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?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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

namespace App\Providers;

use Laravel\Sanctum\Sanctum;
use App\SanctumPersonalAccessClient as PersonalAccessClient;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        //
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();
        Sanctum::usePersonalAccessClientModel(PersonalAccessClient::class);
    }
}

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

namespace App;
use Laravel\Sanctum\PersonalAccessClient;

class SanctumPersonalAccessClient extends PersonalAccessClient
{
    protected $connection = 'my_custom_connection';
    protected $table = 'personal_access_tokens';
}

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda