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

FunCoding's avatar

Laravel 11 Specifying a Custom Model for Sanctum

I need to specify a custom table name and schema for Sanctum. To do this, I need to create a custom model that overrides 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:


<?php

namespace App\Models;

use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;

class CustomPersonalAccessToken extends SanctumPersonalAccessToken
{

    protected $table = 'vendorinv.PersonalAccessToken';
    
}


So the only issue here, is how do I get Laravel to use this model for Sanctum? Any help appreciated, thanks.

0 likes
6 replies
FunCoding's avatar

AI tried to give me an answer, but it didn't work. I created a custom provider and cleared config cache. But that was a no go.

Here is the provider I created:


<?php

namespace App\Providers;

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

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

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {

        Sanctum::usePersonalAccessTokenModel(CustomPersonalAccessToken::class);

    }
    
}


And registered it:

<?php

return [

    App\Providers\AppServiceProvider::class,
    App\Providers\SanctumServiceProvider::class,
    
];

Like I said, this did not work.

FunCoding's avatar

@gych Tried it according to the docs:

<?php

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);

    }
}

Did not work.
FunCoding's avatar

@gych No error other than the expected sql error:

"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 `tokenable_id` = 891) as `exists`)",

It's trying to access the wrong table in the wrong schema. As noted in my original post, I need the schema to be :

vendorinv.PersonalAccessToken

I think it's clear the Sanctum method is being ignored in the provider.

FunCoding's avatar

This issue is solved, it was a simple bug on my end. I was using the DB facade to check for a token, so of course that was going to the wrong DB. Once I used eloquent with the new model, it worked perfectly.

Please or to participate in this conversation.