Installation:
composer require vyuldashev/nova-permission
Ensure spatie/laravel-permission is also installed (follow Spatie's installation guide).
Register the Tool:
Add \Vyuldashev\NovaPermission\NovaPermissionTool::make() to the tools() method in app/Providers/NovaServiceProvider.php.
Configure Middleware:
Add \Vyuldashev\NovaPermission\ForgetCachedPermissions::class to the middleware array in config/nova.php.
Update User Resource:
Modify app/Nova/User.php to include MorphToMany fields for roles and permissions:
use Vyuldashev\NovaPermission\Fields\Permission;
use Vyuldashev\NovaPermission\Fields\Role;
public function fields(Request $request)
{
return [
// ... other fields
Role::make(__('Roles')),
Permission::make(__('Permissions')),
];
}
Publish Assets (if needed):
Run php artisan vendor:publish --provider="Vyuldashev\NovaPermission\NovaPermissionServiceProvider" to publish config or views.
Role and Permission Management:
Admin role with create-post, edit-post, and delete-post permissions.User Assignment:
MorphToMany fields to bulk-assign or remove roles/permissions.Gate/Policy Integration:
gate() and can() methods in your application logic:
if (auth()->user()->can('edit-post')) {
// Allow action
}
NovaPermissionTool::make()->onlyOnRoles(['admin']);
Customizing Fields:
User resource:
Role::make(__('Roles'))
->displayUsing(new CustomRoleDisplay())
->onlyOnDetail(),
Syncing with Existing Data:
syncRoles() or syncPermissions() methods:
$user->syncRoles(['admin', 'editor']);
Nova Tool Configuration:
Customize tool appearance in NovaServiceProvider:
NovaPermissionTool::make()
->title('Custom Permissions')
->icon('lock')
->onlyOnRoles(['super-admin']),
Localization: Publish the language file and translate labels:
php artisan vendor:publish --tag=nova-permission-lang
Testing: Use Spatie’s testing helpers in PHPUnit:
$user->givePermissionTo('edit-post');
$this->assertTrue($user->can('edit-post'));
Performance:
Cache permissions in production (enabled by default via ForgetCachedPermissions middleware). Clear cache when roles/permissions change:
\Spatie\Permission\PermissionRegistrar::forgetCachedPermissions();
Middleware Order:
ForgetCachedPermissions is placed after nova.auth in config/nova.php to avoid permission cache issues:
'middleware' => [
\Laravel\Nova\Http\Middleware\Authenticate::class,
\Laravel\Nova\Http\Middleware\Authorize::class,
\Vyuldashev\NovaPermission\ForgetCachedPermissions::class, // <-- Correct position
],
Field Conflicts:
MorphToMany fields don’t appear, verify:
User model uses HasRoles and HasPermissions traits from Spatie.roles vs. role).Permission Caching:
php artisan cache:clear
config/permission.php during development:
'use_cache' => env('APP_ENV') !== 'local',
Nova Resource Caching:
php artisan nova:cache-reset
Role Hierarchy:
admin inherits editor permissions). Use middleware or policies to enforce this:
if (auth()->user()->hasRole('admin')) {
return true; // Admin has all editor permissions
}
Permission Not Showing:
permission table exists and is populated:
php artisan db:show
model_has_permissions and model_has_roles pivot tables exist.Field Not Rendering:
npm run dev
nova-permission tool is registered in NovaServiceProvider.Mass Assignment Issues:
roles and permissions are in the $fillable or $guarded arrays of the User model.Custom Fields:
Role or Permission fields by creating a custom class:
use Vyuldashev\NovaPermission\Fields\Role as BaseRole;
class CustomRole extends BaseRole
{
public function __construct()
{
parent::__construct();
$this->withMeta(['custom' => 'value']);
}
}
Additional Tools:
class BulkRoleTool extends Tool
{
public function index(Request $request)
{
// Logic to assign roles to multiple users
}
}
API Extensions:
$user = User::find(1);
$user->load('roles', 'permissions');
return response()->json($user);
Event Listeners:
\Spatie\Permission\Events\RoleDeleted::class => [
\App\Listeners\LogPermissionChange::class,
],
Policy Integration:
class PostPolicy
{
public function update(User $user, Post $post)
{
return $user->can('edit-post') && $post->user_id === $user->id;
}
}
How can I help you explore Laravel packages today?