PHP code example of hassni / laravel-nova-permission

1. Go to this page and download the library: Download hassni/laravel-nova-permission library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hassni / laravel-nova-permission example snippets


'providers' => [
    ...,
    Insenseanalytics\LaravelNovaPermission\NovaPermissionServiceProvider::class,
]

// in app/Providers/NovaServiceProvider.php

public function tools()
{
    return [
        // ...
        \Insenseanalytics\LaravelNovaPermission\LaravelNovaPermission::make(),
    ];
}

use Laravel\Nova\Fields\MorphToMany;

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

// in config/nova.php
'middleware' => [
	'web',
	Authenticate::class,
	DispatchServingNovaEvent::class,
	BootTools::class,
	Authorize::class,
	\Insenseanalytics\LaravelNovaPermission\ForgetCachedPermissions::class,
],

php artisan vendor:publish --provider="Insenseanalytics\LaravelNovaPermission\NovaPermissionServiceProvider"

// in app/Providers/NovaServiceProvider.php

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

// in app/Nova/YourNovaResource.php

class YourNovaResource extends Resource
{
    use \Insenseanalytics\LaravelNovaPermission\PermissionsBasedAuthTrait;

    public static $permissionsForAbilities = [
      'all' => 'manage products',
    ];
}

    public static $permissionsForAbilities = [
      'viewAny' => 'view products',
      'view' => 'view products',
      'create' => 'create products',
      'update' => 'update products',
      'delete' => 'delete products',
      'restore' => 'restore products',
      'forceDelete' => 'forceDelete products',
      'addAttribute' => 'add product attributes',
      'attachAttribute' => 'attach product attributes',
      'detachAttribute' => 'detach product attributes',
    ];

    public static $permissionsForAbilities = [
      'addProduct' => 'add user on products'
    ];