vyuldashev / nova-permission by vyuldashev

A Laravel Nova tool for Spatie's Permission library.
1,894,859
411
15
Package Data
Maintainer Username: vyuldashev
Package Create Date: 2018-08-23
Package Last Update: 2023-11-07
Home Page: https://novapackages.com/packages/vyuldashev/nova-permission
Language: PHP
License: MIT
Last Refreshed: 2024-03-28 03:06:24
Package Statistics
Total Downloads: 1,894,859
Monthly Downloads: 43,681
Daily Downloads: 2,371
Total Stars: 411
Total Watchers: 15
Total Forks: 209
Total Open Issues: 42

A Laravel Nova tool for Spatie's laravel-permission library

Latest Version on Packagist Total Downloads

screenshot 1 screenshot 2 screenshot 3

Installation

You can install the package in to a Laravel app that uses Nova via composer:

composer require vyuldashev/nova-permission

Go through the Installation section in order to setup laravel-permission.

Next up, you must register the tool with Nova. This is typically done in the tools method of the NovaServiceProvider.

// in app/Providers/NovaServiceProvider.php

// ...

public function tools()
{
    return [
        // ...
        \Vyuldashev\NovaPermission\NovaPermissionTool::make(),
    ];
}

Next, add middleware to config/nova.php

// in config/nova.php
'middleware' => [
    // ...
    \Vyuldashev\NovaPermission\ForgetCachedPermissions::class,
],

Finally, add MorphToMany fields to you app/Nova/User resource:

// ...
use Laravel\Nova\Fields\MorphToMany;

public function fields(Request $request)
{
    return [
        // ...
        MorphToMany::make('Roles', 'roles', \Vyuldashev\NovaPermission\Role::class),
        MorphToMany::make('Permissions', 'permissions', \Vyuldashev\NovaPermission\Permission::class),
    ];
}

Customization

If you want to use custom resource classes you can define them when you register a tool:

// in app/Providers/NovaServiceProvider.php

// ...

public function tools()
{
    return [
        // ...
        \Vyuldashev\NovaPermission\NovaPermissionTool::make()
            ->roleResource(CustomRole::class)
            ->permissionResource(CustomPermission::class),
    ];
}

Usage

A new menu item called "Permissions & Roles" will appear in your Nova app after installing this package.