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

rajeshtva's avatar

can't create policies when using sanctum guard

i have installed laravel sanctum package for authorization system. everything works fine. i can make api call and other things properly. but when i tried to make a policy for restricting a user to access some data by using command php artisan make:policy QuestionPolicy, it throwed following error.(pa is alias for php artisan i have defined.)

what is causing issue and how can i solve it?

rex@primus:~/rajesh/code/erworks/codalaya/codalaya-laravel-v8$ pa make:policy CategoryPolicy

   LogicException 

  The [sanctum] guard is not defined in your "auth" configuration file.

  at vendor/laravel/framework/src/Illuminate/Foundation/Console/PolicyMakeCommand.php:85
     81▕ 
     82▕         $guard = $this->option('guard') ?: $config->get('auth.defaults.guard');
     83▕ 
     84▕         if (is_null($guardProvider = $config->get('auth.guards.'.$guard.'.provider'))) {
  ➜  85▕             throw new LogicException('The ['.$guard.'] guard is not defined in your "auth" configuration file.');
     86▕         }
     87▕ 
     88▕         return $config->get(
     89▕             'auth.providers.'.$guardProvider.'.model'

      +17 vendor frames 
  18  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()
rex@primus:~/rajesh/code/erworks/codalaya/codalaya-laravel-v8$ 

as i understand i can use sanctum guard without mentioning it in auth.php config file. here is my auth.php file.

<?php

return [

    'defaults' => [
        'guard' => 'sanctum',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],
    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],
    'password_timeout' => 10800,

];
0 likes
6 replies
Nakov's avatar

And you have this in your Kernel.php ?

'api' => [
    \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
    'throttle:api',
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
],

try running php artisan cache:clear in case your config is not picking up.

rajeshtva's avatar

@Nakov i have these lines in App\Http\Kernel.php file. Also i cleared config cache. yet problem persists.

rajeshtva's avatar
rajeshtva
OP
Best Answer
Level 4

@Nakov i figured out what was causing issue.. i have defined default guard sanctum in auth.php. that i was not supposed to define. (laravel sactum mentions no where to do it.). i might have defined it manually. so when i changed it back to web, everything works. kindly write if it was a proper fix.

bmc-atx's avatar

I had the same issue come up in my Laravel 11 project (that I had updated from Laravel 10).

For some reason, the 'defaults' in config/auth had been changed as follows:


    'defaults' => [
        'guard' => env('AUTH_GUARD', 'sanctum'),
        'passwords' => env('AUTH_PASSWORD_BROKER', 'users'),
    ],

This seems to have happened during the update from 10 to 11. When I changed 'defaults' to

'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ]

I was able to use the

php artisan make:policy MyNewPolicy

command again.

1 like

Please or to participate in this conversation.