PHPackages                             sereny/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. sereny/nova-permissions

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

sereny/nova-permissions
=======================

Laravel Nova - Roles &amp; Permissions

v1.7.0(1y ago)86388.6k—5%33[2 PRs](https://github.com/serenysoft/nova-permissions/pulls)1MITPHPPHP ^8.1

Since Dec 15Pushed 6mo ago5 watchersCompare

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

READMEChangelog (10)Dependencies (3)Versions (19)Used By (1)

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

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

[![Latest Version on Packagist](https://camo.githubusercontent.com/dbbafa511e7f9aeacac9fab7bfa7ca587c1787c485497a1ec542819c216b1546/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736572656e792f6e6f76612d7065726d697373696f6e733f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sereny/nova-permissions)[![Total Downloads](https://camo.githubusercontent.com/19166baccbc980afb697cdb7f9c8da965e23f900c7ae638a1a37cba7f5bddca8/68747470733a2f2f706f7365722e707567782e6f72672f736572656e792f6e6f76612d7065726d697373696f6e732f646f776e6c6f6164733f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/sereny/nova-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 your `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:

```
