|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Resources; |
| 4 | + |
| 5 | +use App\Filament\Resources\UserResource\Pages; |
| 6 | +use App\Filament\Resources\UserResource\Pages\CreateUser; |
| 7 | +use App\Filament\Resources\UserResource\Pages\EditUser; |
| 8 | +use App\Models\User; |
| 9 | +use Filament\Forms\Components\Card; |
| 10 | +use Filament\Forms\Components\Group; |
| 11 | +use Filament\Forms\Components\Placeholder; |
| 12 | +use Filament\Forms\Components\TextInput; |
| 13 | +use Filament\Resources\Form; |
| 14 | +use Filament\Resources\Resource; |
| 15 | +use Filament\Resources\Table; |
| 16 | +use Filament\Tables\Actions\DeleteBulkAction; |
| 17 | +use Filament\Tables\Actions\EditAction; |
| 18 | +use Filament\Tables\Columns\TextColumn; |
| 19 | +use Illuminate\Support\Facades\Hash; |
| 20 | +use Illuminate\Support\HtmlString; |
| 21 | +use Illuminate\Validation\Rules\Password; |
| 22 | + |
| 23 | +class UserResource extends Resource |
| 24 | +{ |
| 25 | + protected static ?string $model = User::class; |
| 26 | + |
| 27 | + protected static ?string $navigationIcon = 'heroicon-o-collection'; |
| 28 | + |
| 29 | + protected static ?string $navigationGroup = 'System'; |
| 30 | + |
| 31 | + public static function form(Form $form): Form |
| 32 | + { |
| 33 | + return $form |
| 34 | + ->schema([ |
| 35 | + 'left' => Card::make([ |
| 36 | + 'name' => TextInput::make('name') |
| 37 | + ->required(), |
| 38 | + 'email' => TextInput::make('email') |
| 39 | + ->required() |
| 40 | + ->unique(ignoreRecord: true), |
| 41 | + 'password' => TextInput::make('password') |
| 42 | + ->required() |
| 43 | + ->password() |
| 44 | + ->dehydrateStateUsing(fn ($state) => Hash::make($state)) |
| 45 | + ->visible(fn ($livewire) => $livewire instanceof CreateUser) |
| 46 | + ->rule(Password::default()), |
| 47 | + 'new_password_group' => Group::make([ |
| 48 | + 'new_password' => TextInput::make('new_password') |
| 49 | + ->password() |
| 50 | + ->label('New Password') |
| 51 | + ->nullable() |
| 52 | + ->rule(Password::default()) |
| 53 | + ->visible(fn ($livewire) => $livewire instanceof EditUser) |
| 54 | + ->dehydrated(false), |
| 55 | + 'new_password_confirmation' => TextInput::make('new_password_confirmation') |
| 56 | + ->password() |
| 57 | + ->label('Confirm New Password') |
| 58 | + ->rule('required', fn ($get) => (bool) $get('new_password')) |
| 59 | + ->same('new_password') |
| 60 | + ->visible(fn ($livewire) => $livewire instanceof EditUser) |
| 61 | + ->dehydrated(false), |
| 62 | + ]), |
| 63 | + ])->columnSpan(8), |
| 64 | + 'right' => Card::make([ |
| 65 | + 'created_at' => Placeholder::make('created_at') |
| 66 | + ->content(fn ($record) => $record?->created_at?->diffForHumans() ?? new HtmlString('—')), |
| 67 | + ])->columnSpan(4), |
| 68 | + ]) |
| 69 | + ->columns(12); |
| 70 | + } |
| 71 | + |
| 72 | + public static function table(Table $table): Table |
| 73 | + { |
| 74 | + return $table |
| 75 | + ->columns([ |
| 76 | + TextColumn::make('name'), |
| 77 | + TextColumn::make('email'), |
| 78 | + TextColumn::make('email_verified_at') |
| 79 | + ->dateTime(), |
| 80 | + TextColumn::make('created_at') |
| 81 | + ->dateTime(), |
| 82 | + TextColumn::make('updated_at') |
| 83 | + ->dateTime(), |
| 84 | + ]) |
| 85 | + ->filters([ |
| 86 | + // |
| 87 | + ]) |
| 88 | + ->actions([ |
| 89 | + EditAction::make(), |
| 90 | + ]) |
| 91 | + ->bulkActions([ |
| 92 | + DeleteBulkAction::make(), |
| 93 | + ]); |
| 94 | + } |
| 95 | + |
| 96 | + public static function getRelations(): array |
| 97 | + { |
| 98 | + return [ |
| 99 | + // |
| 100 | + ]; |
| 101 | + } |
| 102 | + |
| 103 | + public static function getPages(): array |
| 104 | + { |
| 105 | + return [ |
| 106 | + 'index' => Pages\ListUsers::route('/'), |
| 107 | + 'create' => Pages\CreateUser::route('/create'), |
| 108 | + 'edit' => Pages\EditUser::route('/{record}/edit'), |
| 109 | + ]; |
| 110 | + } |
| 111 | +} |
0 commit comments