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

kha333n's avatar

Saptie permissions issue on API

I am using spatie permissions library. Sending API call to app which creates a tenant. and that tenant calls a seeder which assign users role. But it gives error: The given role or permission should use guard web instead of sanctum.

Using auth:santum on API route.

    public function run(): void
    {
        $superAdminRole = Role::create(['name' => 'Super Admin']);
        $adminRole = Role::create(['name' => 'Admin']);

        foreach (\App\Enums\System\Permission::cases() as $permission) {
            Permission::create(['name' => $permission]);
        }

        $adminRole->givePermissionTo([
            \App\Enums\System\Permission::READ_USERS,
            \App\Enums\System\Permission::WRITE_USERS,
            \App\Enums\System\Permission::CREATE_USERS,
            \App\Enums\System\Permission::UPDATE_USER_ROLES,
            \App\Enums\System\Permission::READ_ROLES,
            \App\Enums\System\Permission::WRITE_ROLES,
            \App\Enums\System\Permission::CREATE_ROLES,
        ]);
//        dd(auth()->guard());

        User::find(1)->assignRole($superAdminRole); //gives error on this line.
        User::find(2)->assignRole($adminRole);
    }

calling this function via API

    public function create(string $name, string $email, string $domain)
    {
//        try {

            $this->tenant = $this->tenant->create([
                'name' => $name,
                'email' => $email,
            ]);

//        } catch (Exception $e) {
//            throw new Exception(__('vendor/restaurant.create.error.already.exists'));
//        }

        $this->tenant->domains()->create([
            'domain' => $domain,
        ]);

        $this->tenant->createOrGetStripeCustomer();

        return $this->tenant;
    }

Using tenancyforlaravel for tenants. and tenants creation triggers a job which seed DB. It was working on web routes but now not working when called via API.

I tried to create role and permission for guard sanctum. also tried to set getDefaultDriver in users model.

0 likes
1 reply
kha333n's avatar
kha333n
OP
Best Answer
Level 1

In my user model added

    protected array $guard_name = ['api', 'web', 'sanctum'];

and it works... I still don't know what issue is... but it works...

and there is a saying....

If it works. Dont touch it...

1 like

Please or to participate in this conversation.