|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Resources; |
| 4 | + |
| 5 | +use App\Filament\Resources\TeamResource\Pages; |
| 6 | +use App\Filament\Resources\TeamResource\RelationManagers; |
| 7 | +use App\Models\Team; |
| 8 | +use App\Models\User; |
| 9 | +use Filament\Actions\EditAction; |
| 10 | +use Filament\Forms; |
| 11 | +use Filament\Forms\Form; |
| 12 | +use Filament\Resources\Resource; |
| 13 | +use Filament\Tables; |
| 14 | +use Filament\Tables\Table; |
| 15 | +use Illuminate\Database\Eloquent\Builder; |
| 16 | +use Illuminate\Database\Eloquent\SoftDeletingScope; |
| 17 | +use Livewire\Component; |
| 18 | + |
| 19 | +class TeamResource extends Resource |
| 20 | +{ |
| 21 | + protected static ?string $model = Team::class; |
| 22 | + |
| 23 | + protected static ?string $navigationLabel = 'Teams'; |
| 24 | + protected static ?string $navigationIcon = 'heroicon-o-users'; |
| 25 | + protected static ?int $navigationSort = 3; |
| 26 | + |
| 27 | + protected static ?string $slug = 'teams'; |
| 28 | + protected static ?string $navigationGroup = 'Management'; |
| 29 | + |
| 30 | + |
| 31 | + public static function form(Form $form): Form |
| 32 | + { |
| 33 | + return $form |
| 34 | + ->schema([ |
| 35 | + Forms\Components\TextInput::make('name') |
| 36 | + ->required() |
| 37 | + ->maxLength(255), |
| 38 | + // Benutzer, der das Team erstellt, als "Besitzer" des Teams setzen |
| 39 | + Forms\Components\TextInput::make('personal_team') |
| 40 | + ->default(fn () => false) |
| 41 | + ->hidden(), |
| 42 | + Forms\Components\Select::make('users') |
| 43 | + ->label('Mitglieder') |
| 44 | + ->multiple() |
| 45 | + ->relationship('users', 'name') |
| 46 | + ->preload(), |
| 47 | + ]); |
| 48 | + } |
| 49 | + |
| 50 | + public static function table(Table $table): Table |
| 51 | + { |
| 52 | + return $table |
| 53 | + ->columns([ |
| 54 | + Tables\Columns\TextColumn::make('name') |
| 55 | + ->searchable() |
| 56 | + ->sortable(), |
| 57 | + Tables\Columns\TextColumn::make('users.name') |
| 58 | + ->label('Mitglieder') |
| 59 | + ->limit(3), // Zeige maximal 3 User-Namen |
| 60 | + Tables\Columns\TextColumn::make('created_at') |
| 61 | + ->label('Created') |
| 62 | + ->sortable() |
| 63 | + ->dateTime(), |
| 64 | + Tables\Columns\TextColumn::make('updated_at') |
| 65 | + ->label('Updated') |
| 66 | + ->sortable() |
| 67 | + ->dateTime(), |
| 68 | + ]) |
| 69 | + ->filters([ |
| 70 | + ]) |
| 71 | + ->actions([ |
| 72 | + Tables\Actions\EditAction::make(), |
| 73 | + Tables\Actions\Action::make('delete') |
| 74 | + ->action(function (Team $record) { |
| 75 | + $record->delete(); |
| 76 | + }) |
| 77 | + ->requiresConfirmation(), |
| 78 | + ]) |
| 79 | + ->bulkActions([ |
| 80 | + Tables\Actions\BulkAction::make('delete') |
| 81 | + ->action(function (array $records) { |
| 82 | + Team::destroy($records); |
| 83 | + }) |
| 84 | + ->requiresConfirmation(), |
| 85 | + ]); |
| 86 | + } |
| 87 | + |
| 88 | + public static function getRelations(): array |
| 89 | + { |
| 90 | + return [ |
| 91 | + RelationManagers\UsersRelationManager::class, |
| 92 | + ]; |
| 93 | + } |
| 94 | + |
| 95 | + public static function getPages(): array |
| 96 | + { |
| 97 | + return [ |
| 98 | + 'index' => Pages\ListTeams::route('/'), |
| 99 | + 'create' => Pages\CreateTeam::route('/create'), |
| 100 | + 'edit' => Pages\EditTeam::route('/{record}/edit'), |
| 101 | + ]; |
| 102 | + } |
| 103 | +} |
0 commit comments