Until now, we checked if the user can do something by checking if users.is_admin
or users.is_accountant
is true.
This can be done better by using roles and permissions. And Filament has an excellent plugin for that called Shield which under the hood uses spatie/laravel-permission package.
This lesson will use the Shield package to add roles and permissions functionality.
Composer Install Package
First, let's install the package via composer.
composer require bezhansalleh/filament-shield "^3.0@beta"
Then we need to add Spatie\Permission\Traits\HasRoles
trait to the User
Model.
app/Models/User.php:
use Spatie\Permission\Traits\HasRoles; class User extends Authenticatable implements FilamentUser{ use HasApiTokens, HasFactory, Notifiable; use HasRoles; // ...}
Next, we need to register this package in the panel.
app/Providers/Filament/AdminPanelProvider.php:
class AdminPanelProvider extends PanelProvider{ public function panel(Panel $panel): Panel { return $panel // ... ->plugins([ \BezhanSalleh\FilamentShield\FilamentShieldPlugin::make() ]); }}
If you have multiple panels, you may choose to add it only for admin users. This way, only users with access to...