PHPackages                             kazak71/nova-permissions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. kazak71/nova-permissions

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

kazak71/nova-permissions
========================

Laravel Nova 4 Grouped Permissions

01PHP

Since Mar 14Pushed 2y agoCompare

[ Source](https://github.com/kazak71/nova-permissions)[ Packagist](https://packagist.org/packages/kazak71/nova-permissions)[ RSS](/packages/kazak71-nova-permissions/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Nova 4 Roles &amp; Permissions
======================================

[](#laravel-nova-4-roles--permissions)

A Laravel Nova Tool that allows you to group your Permissions and attach it to Users. It uses Spatie's laravel-permission.

We have a Migration, Seed, Policy and Resource ready for a good Authorization Experience.

1. [Installation](#Installation)
2. [Permissions with Groups](#permissions-with-groups)
    - [Index view](#index-view)
    - [Detail View](#detail-view)
    - [Edit View](#edit-view)
    - [Database Seeding](#database-seeding)
    - [Create a Model Policy](#create-a-model-policy)
    - [Super Admin](#super-admin)
3. [Customization](#customization)
4. [Credits](#credits)

Installation
------------

[](#installation)

You can install the package in to a Laravel app that uses [Nova](https://nova.laravel.com) via composer:

```
composer require sereny/nova-permissions
```

Publish the Migration with the following command:

```
php artisan vendor:publish --provider="Sereny\NovaPermissions\ToolServiceProvider" --tag="migrations"
```

Migrate the Database:

```
php artisan migrate
```

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 [
        // ...
        new \Sereny\NovaPermissions\NovaPermissions(),
    ];
}
```

If you want to hide the tool from certain users, you can write your custom logic for the ability to see the tool:

```
// in app/Providers/NovaServiceProvider.php

// ...

public function tools()
{
    return [
        // ...
        (new \Sereny\NovaPermissions\NovaPermissions())->canSee(function ($request) {
            return $request->user()->isSuperAdmin();
        }),
    ];
}
```

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', \Sereny\NovaPermissions\Nova\Role::class),
        MorphToMany::make('Permissions', 'permissions', \Sereny\NovaPermissions\Nova\Permission::class),
    ];
}
```

Add the Spatie\\Permission\\Traits\\HasRoles trait to your User model(s):

```
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;

    // ...
}
```

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

Permissions with Groups
-----------------------

[](#permissions-with-groups)

### Index View

[](#index-view)

[![image](/.github/images/role-index.png)](/.github/images/role-index.png)

### Detail View

[](#detail-view)

[![image](/.github/images/role-detail.png)](/.github/images/role-detail.png)

### Edit View

[](#edit-view)

[![image](/.github/images/role-edit.png)](/.github/images/role-edit.png)

### Database Seeding

[](#database-seeding)

Publish our Seeder with the following command:

```
php artisan vendor:publish --provider="Sereny\NovaPermissions\ToolServiceProvider" --tag="seeders"

```

This is just an example on how you could seed your Database with Roles and Permissions. Modify `RolesAndPermissionsSeeder.php` in `database/seeders`. List all your Models you want to have Permissions for in the `$collection` Array and change the email for the Super-Admin:

```
