|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Resources; |
| 4 | + |
| 5 | +use App\Filament\Resources\PersonalAccessTokenResource\Pages; |
| 6 | +use Filament\Forms; |
| 7 | +use Filament\Forms\Form; |
| 8 | +use Filament\Resources\Resource; |
| 9 | +use Filament\Tables; |
| 10 | +use Filament\Tables\Table; |
| 11 | +use Laravel\Sanctum\PersonalAccessToken; |
| 12 | + |
| 13 | +class PersonalAccessTokenResource extends Resource |
| 14 | +{ |
| 15 | + protected static ?string $model = PersonalAccessToken::class; |
| 16 | + |
| 17 | + protected static ?string $navigationIcon = 'heroicon-o-key'; |
| 18 | + |
| 19 | + protected static ?string $navigationLabel = 'API Keys'; |
| 20 | + |
| 21 | + protected static ?string $modelLabel = 'API Key'; |
| 22 | + |
| 23 | + protected static ?string $pluralModelLabel = 'API Keys'; |
| 24 | + |
| 25 | + public static function form(Form $form): Form |
| 26 | + { |
| 27 | + return $form |
| 28 | + ->schema([ |
| 29 | + Forms\Components\TextInput::make('name') |
| 30 | + ->required() |
| 31 | + ->maxLength(255) |
| 32 | + ->helperText('A descriptive name for this API key'), |
| 33 | + |
| 34 | + Forms\Components\Select::make('tokenable_id') |
| 35 | + ->label('User') |
| 36 | + ->options(\App\Models\User::pluck('name', 'id')) |
| 37 | + ->required() |
| 38 | + ->searchable(), |
| 39 | + |
| 40 | + Forms\Components\Hidden::make('tokenable_type') |
| 41 | + ->default(\App\Models\User::class), |
| 42 | + |
| 43 | + Forms\Components\TextInput::make('abilities') |
| 44 | + ->default('*') |
| 45 | + ->helperText('Comma-separated list of abilities. Use * for all abilities.'), |
| 46 | + |
| 47 | + Forms\Components\DateTimePicker::make('expires_at') |
| 48 | + ->label('Expires At') |
| 49 | + ->nullable() |
| 50 | + ->helperText('Leave empty for no expiration'), |
| 51 | + ]); |
| 52 | + } |
| 53 | + |
| 54 | + public static function table(Table $table): Table |
| 55 | + { |
| 56 | + return $table |
| 57 | + ->columns([ |
| 58 | + Tables\Columns\TextColumn::make('name') |
| 59 | + ->searchable() |
| 60 | + ->sortable(), |
| 61 | + |
| 62 | + Tables\Columns\TextColumn::make('tokenable.name') |
| 63 | + ->label('User') |
| 64 | + ->searchable() |
| 65 | + ->sortable(), |
| 66 | + |
| 67 | + Tables\Columns\TextColumn::make('abilities') |
| 68 | + ->formatStateUsing(fn ($state) => is_array($state) ? implode(', ', $state) : $state), |
| 69 | + |
| 70 | + Tables\Columns\TextColumn::make('last_used_at') |
| 71 | + ->dateTime() |
| 72 | + ->sortable() |
| 73 | + ->placeholder('Never'), |
| 74 | + |
| 75 | + Tables\Columns\TextColumn::make('expires_at') |
| 76 | + ->dateTime() |
| 77 | + ->sortable() |
| 78 | + ->placeholder('Never'), |
| 79 | + |
| 80 | + Tables\Columns\TextColumn::make('created_at') |
| 81 | + ->dateTime() |
| 82 | + ->sortable() |
| 83 | + ->toggleable(isToggledHiddenByDefault: true), |
| 84 | + ]) |
| 85 | + ->filters([ |
| 86 | + // |
| 87 | + ]) |
| 88 | + ->actions([ |
| 89 | + Tables\Actions\DeleteAction::make(), |
| 90 | + ]) |
| 91 | + ->bulkActions([ |
| 92 | + Tables\Actions\DeleteBulkAction::make(), |
| 93 | + ]) |
| 94 | + ->defaultSort('created_at', 'desc'); |
| 95 | + } |
| 96 | + |
| 97 | + public static function getRelations(): array |
| 98 | + { |
| 99 | + return [ |
| 100 | + // |
| 101 | + ]; |
| 102 | + } |
| 103 | + |
| 104 | + public static function getPages(): array |
| 105 | + { |
| 106 | + return [ |
| 107 | + 'index' => Pages\ListPersonalAccessTokens::route('/'), |
| 108 | + 'create' => Pages\CreatePersonalAccessToken::route('/create'), |
| 109 | + ]; |
| 110 | + } |
| 111 | +} |
0 commit comments