Filament: Roles with Spatie Permission Package

Demonstrates how to use spatie/laravel-permission package in Filament and auto-assign a role when the user registers for the application.

filament-examples-roles-and-permissions

Get the Source Code:

How it works

The registration form is enabled in AdminPanelProvider.

app/Providers/Filament/AdminPanelProvider.php:

class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->default()
->id('admin')
->path('admin')
->login()
->registration() // here we enable registration feature
// ...
}
}

The spatie/laravel-permission package is used for the roles and permissions. Two roles are seeded: admin and user.

When a new user registers to the application, the user role is assigned using the closure Eloquent event created.

app/Models/User.php:

class User extends Authenticatable implements FilamentUser
{
// ...
 
protected static function booted(): void
{
static::created(function (User $user) {
$user->assignRole('user');
});
}
 
// ...
}

In the Admin Panel, we have two Filament resources: TaskResource and UserResource.

The UserResource menu item "Users" can only be seen by the user with an admin role.

For that...

The FULL tutorial is available after the purchase: in the Readme file of the official repository you would get invited to.
Get the Source Code: All 51 Premium Examples for $99 Only Project Examples for $59