A Laravel Nova tool for managing roles and permissions via Spatie Laravel Permission
iamgerwin/nova-spatie-role-permission is a Laravel package for a laravel nova tool for managing roles and permissions via spatie laravel permission.
It currently has 2 GitHub stars and 4.644 downloads on Packagist (latest version v1.1.7).
Install it with composer require iamgerwin/nova-spatie-role-permission.
Discover more Laravel packages by iamgerwin
or browse all Laravel packages to compare alternatives.
Last updated
A comprehensive Laravel Nova tool for managing roles and permissions with Spatie's Laravel Permission package. Built for Laravel 11-12, Nova 5, and PHP 8.3+.
Install the package via composer:
composer require iamgerwin/nova-spatie-role-permission
The package includes default configuration, but you can publish it for customization:
php artisan vendor:publish --provider="Iamgerwin\NovaSpatieRolePermission\ToolServiceProvider"
After installation, clear all caches to ensure proper loading:
php artisan config:clear
php artisan cache:clear
Publish and run the migrations from Spatie Laravel Permission if you haven't already:
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
php artisan migrate
Register the tool in your NovaServiceProvider:
// app/Providers/NovaServiceProvider.php
use Iamgerwin\NovaSpatieRolePermission\NovaSpatieRolePermissionTool;
public function tools()
{
return [
new NovaSpatieRolePermissionTool(),
];
}
Note: Laravel Nova must be installed and configured in your application before using this package's features.
For a faster setup, use the installation command which handles most of the above steps:
php artisan nova-permission:install
This command will:
You can also use command options for non-interactive installation:
# Install with default permissions and super-admin role
php artisan nova-permission:install --seed
# Install and assign super-admin to a specific user
php artisan nova-permission:install --seed [email protected]
Publish the configuration file to customize the package:
php artisan vendor:publish --tag=nova-permission-config
This will create config/nova-permission.php where you can customize:
After installation, verify everything is properly configured:
php artisan nova-permission:verify
This command will check:
You can add role and permission fields to your User Nova resource in multiple ways:
// app/Nova/User.php
use Laravel\Nova\Fields\MorphToMany;
use Iamgerwin\NovaSpatieRolePermission\Nova\Role;
use Iamgerwin\NovaSpatieRolePermission\Nova\Permission;
public function fields(NovaRequest $request)
{
return [
// ... other fields
MorphToMany::make('Roles', 'roles', Role::class),
MorphToMany::make('Permissions', 'permissions', Permission::class),
];
}
For a checkbox-style interface:
use Iamgerwin\NovaSpatieRolePermission\Fields\RoleBooleanGroup;
use Iamgerwin\NovaSpatieRolePermission\Fields\PermissionBooleanGroup;
public function fields(NovaRequest $request)
{
return [
// ... other fields
RoleBooleanGroup::make('Roles', 'roles')
->options(app(config('permission.models.role'))->pluck('name', 'id')->toArray()),
PermissionBooleanGroup::make('Permissions', 'permissions')
->options(app(config('permission.models.permission'))->pluck('name', 'id')->toArray()),
];
}
use Iamgerwin\NovaSpatieRolePermission\Fields\RoleSelect;
public function fields(NovaRequest $request)
{
return [
// ... other fields
RoleSelect::make('Role', 'role')
->options(app(config('permission.models.role'))->pluck('name', 'id')->toArray())
->displayUsingLabels(),
];
}
You can use your own Nova resources:
use App\Nova\CustomRole;
use App\Nova\CustomPermission;
public function tools()
{
return [
(new NovaSpatieRolePermissionTool())
->roleResource(CustomRole::class)
->permissionResource(CustomPermission::class),
];
}
Define your own authorization policies:
use App\Policies\CustomRolePolicy;
use App\Policies\CustomPermissionPolicy;
public function tools()
{
return [
(new NovaSpatieRolePermissionTool())
->rolePolicy(CustomRolePolicy::class)
->permissionPolicy(CustomPermissionPolicy::class),
];
}
Specify custom guards for roles and permissions:
public function tools()
{
return [
(new NovaSpatieRolePermissionTool())
->roleGuard('admin')
->permissionGuard('api'),
];
}
The package includes automatic permission cache clearing. To enable it, add the middleware to your Nova middleware group:
// app/Http/Kernel.php
protected $middlewareGroups = [
'nova' => [
// ... other middleware
\Iamgerwin\NovaSpatieRolePermission\Http\Middleware\ForgetCachedPermissions::class,
],
];
The package includes a bulk action to attach permissions to roles:
use Iamgerwin\NovaSpatieRolePermission\Actions\AttachToRole;
public function actions(NovaRequest $request)
{
return [
new AttachToRole,
];
}
The package supports internationalization. Publish the language files:
php artisan vendor:publish --tag="nova-spatie-role-permission-translations"
Available languages:
You can add your own translations in the resources/lang/vendor/nova-spatie-role-permission directory.
As of version 1.1.0, the package includes secure default policies that check for actual permissions. The policies support granular permissions and a super admin role that bypasses all checks.
The package's policies check for these permissions:
view-roles - View role listings and detailscreate-roles - Create new rolesedit-roles - Update existing rolesdelete-roles - Delete rolesrestore-roles - Restore soft-deleted rolesforce-delete-roles - Permanently delete rolesassign-permissions - Attach permissions to rolesrevoke-permissions - Detach permissions from rolesmanage-roles - Full role management (grants all role permissions)view-permissions - View permission listings and detailscreate-permissions - Create new permissionsedit-permissions - Update existing permissionsdelete-permissions - Delete permissionsrestore-permissions - Restore soft-deleted permissionsforce-delete-permissions - Permanently delete permissionsassign-roles - Attach roles to permissionsrevoke-roles - Detach roles from permissionsmanage-permissions - Full permission management (grants all permission permissions)super-admin role bypass all authorization checksTo override the default policies with your own logic:
// app/Policies/RolePolicy.php
namespace App\Policies;
use App\Models\User;
use Spatie\Permission\Models\Role;
class RolePolicy
{
public function before(User $user, $ability): ?bool
{
// Super admin bypasses all checks
if ($user->hasRole('super-admin')) {
return true;
}
return null;
}
public function viewAny(User $user): bool
{
return $user->hasPermissionTo('view-roles');
}
public function create(User $user): bool
{
return $user->hasPermissionTo('create-roles');
}
// ... other methods
}
public function tools()
{
return [
(new NovaSpatieRolePermissionTool())
->rolePolicy(App\Policies\RolePolicy::class)
->permissionPolicy(App\Policies\PermissionPolicy::class),
];
}
composer test
Run code formatting:
composer format
Run static analysis:
composer analyse
Note: Static analysis excludes Nova-dependent files since Nova is not available during package development. These files are fully functional when Nova is installed in your application.
This error occurs when the configuration file is not properly loaded. Solutions:
php artisan vendor:publish --provider="Iamgerwin\NovaSpatieRolePermission\ToolServiceProvider"
php artisan config:clear
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
If the package isn't working after installation:
# Clear all caches
composer dump-autoload
php artisan config:clear
php artisan cache:clear
php artisan nova:publish
# Verify installation
php artisan nova-permission:verify
If permissions or roles aren't showing up:
php artisan migrate:status
php artisan permission:cache-reset
php artisan nova-permission:verify
If users can't access roles/permissions despite having the right permissions:
// Check in tinker
php artisan tinker
>>> User::find(1)->hasPermissionTo('manage-roles')
php artisan permission:cache-reset
php artisan nova-permission:verify
Use the verification command to diagnose installation issues:
php artisan nova-permission:verify
This command checks:
If you're still experiencing issues:
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
This package is based on the original work by vyuldashev/nova-permission and has been updated and enhanced for modern Laravel, Nova, and PHP versions.
The MIT License (MIT). Please see License File for more information.