All packages
Permissions for Laravel
Exceptions
Every error the package raises extends PermissionException (a RuntimeException), so a single catch handles the whole family:
| Exception | Thrown when |
|---|---|
PermissionDoesNotExist | Granting, revoking or syncing a permission name that is not registered. |
RoleDoesNotExist | Assigning, removing or syncing a role name that is not registered. |
PermissionException | A grant argument is not a string, a backed enum or the expected model, or a model instance has not been saved yet; the holder lacks HasRoles (role writes) or HasPermissions (permission writes) or has no key; syncFrom() got a class that is not a backed enum. Also the base class of the two above. |
use RoundlyConsulting\Permissions\Exceptions\PermissionDoesNotExist;
use RoundlyConsulting\Permissions\Exceptions\PermissionException;
use RoundlyConsulting\Permissions\Exceptions\RoleDoesNotExist;
use RoundlyConsulting\Permissions\Facades\Permissions;
use RoundlyConsulting\Permissions\Models\Permission;
try {
Permissions::for($user)->assignRole('auditor');
} catch (RoleDoesNotExist $e) {
// There is no role named "auditor".
}
try {
Permissions::for($role)->givePermissionTo('reports.export');
} catch (PermissionDoesNotExist $e) {
// There is no permission named "reports.export".
}
try {
Permissions::for($role)->givePermissionTo(new Permission(['name' => 'draft']));
} catch (PermissionException $e) {
// Cannot grant an unsaved permission model; persist it before granting.
}
try {
Permissions::for($role)->assignRole('editor');
} catch (PermissionException $e) {
// A Role holds permissions, never roles — it has no HasRoles trait.
}Messages go through Laravel’s translator, so you can translate them in your own lang files. Checks — hasRole() and hasPermissionTo() — never throw; an unknown name returns false.
Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.