Adding Role and Permission In Filament using Filament Shield (Filament Series — Part 5)

muhammadhalimdirgantara
3 min readFeb 18, 2024

--

Read previous article, Creating User Resource (User CRUD) using Laravel Filament (Filament Series -Part 4) before you read this!

Laravel Permission is one of the most powerful packages in Laravel to manage user roles and permissions. Spatie’s Laravel Permission makes implementing RBAC (role-based access control) easier than ever before.

Laravel Permission is a powerful package for defining roles, assigning permissions, and managing user access in your Laravel apps. It comes with an easy-to-use syntax and powerful features.

In Filament, there is a plugin that can be used to manage the permissions of the Resources, Pages and Widgets in the Filament Panel using spatie / Laravel-permission.

Filament Shield created by Bezhan Salleh an expert full-stack developer. Using Filament Shield you can easy manage role and permission that support latest spatie / Laravel-permission.

The first thing we need to do if we want to install Filament Shield for our project is to run the following command :

composer require bezhansalleh/filament-shield
filament shield installation process

Now we need to add the Spatie\Permission\Traits\HasRoles trait to our User model.

use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
use HasRoles;

And then we publish the config file then setup our configuration using following command :

php artisan vendor:publish --tag=filament-shield-config
publish the filament shield config file

Don’t Forget to register the plugin for the Filament Panels you want in the app\Providers\Filament\AdminPanelProvider.php file using this syntax,

->plugins([
\BezhanSalleh\FilamentShield\FilamentShieldPlugin::make()
])

Finally, to install the filament shield into our project, we execute the following command:

php artisan shield:install
filament shield installation process

Now, we have new Roles menu with roles table and the default role is Super Admin,

filament shield roles menu

When we create a role, we’ll see the role creation form that contains all the models and resources we’ve created, along with permission for all the roles and actions they can and can’t do.

In the next post, we’re going to start building a simple blog with categories, tags and post menu. Keep reading as we get into the nitty-gritty of building this crucial part of web development with Filament!

I’m a web developer, server administrator, and system analyst experienced in Laravel. I work for the Sekadau Region Government in Indonesia, leading web app development. Check out my work on Medium (https://medium.com/@halimdirgantara) and GitHub (https://github.com/halimdirgantara/).

--

--

muhammadhalimdirgantara

I'm a web developer, server administrator, and system analyst experienced in Laravel. I work for the Sekadau Region Government in Indonesia.