Bill Keck
Bill Keck

Reputation: 1

Laravel 11 Specifying a Custom Model for Sanctum

I need to specify a custom table name and schema for Sanctum in Laravel 11 because we use multiple schemas. To do this, I must create a custom model overriding PersonAccessToken.

Sanctum provides a method for specifying the model:

Sanctum::userPersonalAccessModelTokenModel(CustomPersonalAccessToken::class);

Previously, this would have gone into AuthServiceProvider.php in the boot method, but with Laravel 11, that file is no longer there. I need to know how to make Laravel aware of the class I want to use. Here is the class for reference:

namespace App\Models;

use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;

class CustomPersonalAccessToken extends SanctumPersonalAccessToken
{
    protected $table = 'vendorinv.PersonalAccessToken';
}

I've tried the solution from the docs by adding it to the service provider:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Laravel\Sanctum\Sanctum;
use App\Models\CustomPersonalAccessToken;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
Sanctum::usePersonalAccessTokenModel(CustomPersonalAccessToken::class);
    }
}

I think it's clear the provider is ignoring the Sanctum method. Any help would be appreciated. Thank you

The only error I get is expected because it's trying to reach the wrong schema and table:

"message": "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'websites.personal_access_tokens' doesn't exist (Connection: mysql, SQL: select exists(select * from personal_access_tokens where

I need it to reach:

vendorinv.PersonalAccessToken

Any help will be appreciated. Thank you.

Upvotes: 0

Views: 851

Answers (1)

Bill Keck
Bill Keck

Reputation: 1

The bug was on my end, I was using the DB facade to do a check on the token in my login method. So of course that was checking the wrong db. Once I switched to using the new model, everything worked. Thanks for the helpful comments, they pointed me in the right direction.

Upvotes: 0

Related Questions