Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kerrymoralee's avatar

Extending Spatie Roles & Permissions

Hi

I’m trying to extend Spatie Roles in Filament. I’ve got the model and the config is all pointing to the correct place as the navigation icon is set in my extended model. But how do I change the table layout - I would normally do it in a resource, so how do I extend the Spatie Roles Resource?

0 likes
4 replies
kerrymoralee's avatar

Sorry, it's not a model I've got, I've extended the RoleResource, and as I say it must be working because I can set the activenavigation icon etc, but I can't change the table. If I put in the resource a custom table like I would any other resource, nothing changes. What am I missing?

LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To extend the Spatie Roles in Filament and customize the table layout in a resource, you can follow these steps:

  1. Extend the Role Model: First, ensure that you have extended the Spatie Role model correctly. You should have a model that extends Spatie\Permission\Models\Role. For example:

    namespace App\Models;
    
    use Spatie\Permission\Models\Role as SpatieRole;
    
    class Role extends SpatieRole
    {
        // You can customize the model here
    }
    
  2. Update config/permission.php: Make sure that the role model points to your new extended model in the config/permission.php.

    'models' => [
        'role' => App\Models\Role::class,
    ],
    
  3. Create a Filament Resource for Role: If you haven't already, create a Filament resource for the Role model. You can use the Filament CLI to generate it:

    php artisan make:filament-resource Role
    
  4. Customize the Resource: In the generated Role resource, you can customize the table layout by modifying the getTable method. Here’s an example of how you might customize the columns:

    namespace App\Filament\Resources;
    
    use App\Filament\Resources\RoleResource\Pages;
    use App\Filament\Resources\RoleResource\RelationManagers;
    use App\Models\Role;
    use Filament\Forms;
    use Filament\Resources\Form;
    use Filament\Resources\Resource;
    use Filament\Resources\Table;
    use Filament\Tables;
    
    class RoleResource extends Resource
    {
        protected static ?string $model = Role::class;
    
        protected static ?string $navigationIcon = 'heroicon-o-shield-check';
    
        public static function form(Form $form): Form
        {
            return $form
                ->schema([
                    Forms\Components\TextInput::make('name')
                        ->required()
                        ->unique(ignoreRecord: true),
                    Forms\Components\TextInput::make('guard_name')
                        ->default('web')
                        ->required(),
                ]);
        }
    
        public static function table(Table $table): Table
        {
            return $table
                ->columns([
                    Tables\Columns\TextColumn::make('name')->sortable(),
                    Tables\Columns\TextColumn::make('guard_name')->sortable(),
                ])
                ->filters([
                    //
                ]);
        }
    
        public static function getPages(): array
        {
            return [
                'index' => Pages\ListRoles::route('/'),
                'create' => Pages\CreateRole::route('/create'),
                'edit' => Pages\EditRole::route('/{record}/edit'),
            ];
        }
    }
    
  5. Adjust Navigation: Ensure that your new Role resource is correctly linked in the Filament navigation by editing the getNavigation method if needed.

  6. Migrate and Test: After setting up your extended model and resource, run any necessary migrations and test your application to ensure that everything is working as expected.

This setup allows you to fully leverage Filament's capabilities while using the extended Spatie permission system.

kerrymoralee's avatar

Above is pretty much what I've done, except for extending a Resource (results in error), I've extended Spatie's RoleResource.

...

class RoleResource extends \Althinect\FilamentSpatieRolesPermissions\Resources\RoleResource
{
    protected static ?string $tenantOwnershipRelationshipName = 'academy';

    protected static ?string $tenantRelationshipName = 'members';

    protected static ?string $activeNavigationIcon = 'heroicon-s-lock-open';

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->searchable()
                    ->sortable(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }
}

Any ideas anyone?

kerrymoralee's avatar

Actually, I've just taken another look at that and there were a few other things I was missing. Seems to be working now I've followed more closely.

How would I amend what data is returned for the list items. What I'm trying to show in the list table is both the roles assigned to that team (multi tenancy) and also the roles that are global (no team_id assigned).

Please or to participate in this conversation.