Skip to content

givePermissionTo() throws exception on missing permission without fallback option #2858

@shakin-shahria

Description

@shakin-shahria

Description

Hi,
I ran into an issue using givePermissionTo(). If I try to assign a permission that doesn’t exist in the database, it throws this error:
Spatie\Permission\Exceptions\PermissionDoesNotExist: There is no permission named xyzfor guardweb.
This becomes a problem when assigning dynamic permissions (e.g., from a form or seed data) — I have to wrap every call in a try-catch, which makes the code messy.

Steps To Reproduce

1.A givePermissionIfExistsTo() method, or
2.A config option to skip missing permissions
Would love to hear your thoughts on this or if there's a better way to handle it.
Thanks for the great package!

Example Application

No response

Version of spatie/laravel-permission package:

6.11.0

Version of laravel/framework package:

10.42

PHP version:

8.2

Database engine and version:

MySQL 8.0

OS: Windows/Mac/Linux version:

Windows 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions