How to Create Roles and Permissions in Laravel with Filament Shield

How to Create Roles and Permissions in Laravel with Filament Shield

In this guide, we'll continue building on our Laravel post project by implementing roles and permissions using the Filament Shield package.

Step 1: Install the Filament Shield Package

Run the following command to install the package:

composer require bezhansalleh/filament-shield

Step 2: Configure the Auth Provider

2.1 Publish the Configuration

Publish the package's configuration file and set up your auth provider model:

php artisan vendor:publish --tag="filament-shield-config"

Update the config/filament-shield.php file to specify your auth provider model:

// config/filament-shield.php  
'auth_provider_model' => [  
    'fqcn' => 'App\\Models\\User',  
],

2.2 Add the HasRoles Trait

Modify your User model to include the HasRoles trait provided by the Spatie Permission package:

use Spatie\Permission\Traits\HasRoles;  

class User extends Authenticatable  
{  
    use HasRoles;  
}

Step 3: Set Up the Filament Shield Package

Run the following command to set up the Filament Shield package:

php artisan shield:setup

Step 4: Install the Package for the Admin Panel

The installation command automatically registers the plugin for your Filament panel. If you're not using tenancy, use the following command:

php artisan shield:install admin

Replace 'admin' with the ID of your Filament panel.

And that's it! The roles, permissions, and a dedicated menu have been successfully added to your admin panel.

Now, you can manage roles and permissions directly from the admin panel, making it easier to control access to different parts of your application.