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?
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?
To extend the Spatie Roles in Filament and customize the table layout in a resource, you can follow these steps:
-
Extend the Role Model: First, ensure that you have extended the Spatie
Rolemodel correctly. You should have a model that extendsSpatie\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 } -
Update
config/permission.php: Make sure that therolemodel points to your new extended model in theconfig/permission.php.'models' => [ 'role' => App\Models\Role::class, ], -
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 -
Customize the Resource: In the generated Role resource, you can customize the table layout by modifying the
getTablemethod. 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'), ]; } } -
Adjust Navigation: Ensure that your new Role resource is correctly linked in the Filament navigation by editing the
getNavigationmethod if needed. -
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.
Please or to participate in this conversation.