44
55use App \Enums \UserRole ;
66use App \Filament \Resources \UserResource \Pages ;
7- use App \Filament \Resources \UserResource \Pages \CreateUser ;
8- use App \Filament \Resources \UserResource \Pages \EditUser ;
97use App \Models \User ;
108use Filament \Forms ;
119use Filament \Forms \Form ;
1412use Filament \Tables \Table ;
1513use Illuminate \Support \Facades \Auth ;
1614use Illuminate \Support \Facades \Hash ;
17- use Illuminate \Support \HtmlString ;
18- use Illuminate \Validation \Rules \Password ;
1915
2016class UserResource extends Resource
2117{
@@ -29,77 +25,72 @@ public static function form(Form $form): Form
2925 ->schema ([
3026 Forms \Components \Grid::make ([
3127 'default ' => 1 ,
32- 'md ' => 3 ,
33- ])
28+ ])->columnSpan ([
29+ 'lg ' => 2 ,
30+ ])->schema ([
31+ Forms \Components \Section::make ('Details ' )
32+ ->columns ([
33+ 'default ' => 1 ,
34+ 'lg ' => 2 ,
35+ ])
36+ ->schema ([
37+ Forms \Components \TextInput::make ('name ' )
38+ ->required ()
39+ ->maxLength (255 )
40+ ->columnSpanFull (),
41+
42+ Forms \Components \TextInput::make ('email ' )
43+ ->email ()
44+ ->required ()
45+ ->maxLength (255 )
46+ ->columnSpanFull (),
47+
48+ Forms \Components \TextInput::make ('password ' )
49+ ->confirmed ()
50+ ->password ()
51+ ->revealable ()
52+ ->required (fn (string $ context ): bool => $ context === 'create ' )
53+ ->dehydrateStateUsing (fn ($ state ) => Hash::make ($ state ))
54+ ->dehydrated (fn ($ state ) => filled ($ state )),
55+
56+ Forms \Components \TextInput::make ('password_confirmation ' )
57+ ->password ()
58+ ->revealable (),
59+
60+ // ...
61+ ]),
62+ ]),
63+
64+ Forms \Components \Grid::make (1 )
65+ ->columnSpan (1 )
3466 ->schema ([
35- Forms \Components \Section::make ()
67+ Forms \Components \Section::make (' Platform ' )
3668 ->schema ([
37- Forms \Components \TextInput::make ('name ' )
38- ->required (),
39- Forms \Components \TextInput::make ('email ' )
40- ->required ()
41- ->email ()
42- ->unique (ignoreRecord: true ),
43- Forms \Components \TextInput::make ('password ' )
69+ Forms \Components \Select::make ('role ' )
70+ ->label ('Role ' )
71+ ->default (UserRole::User)
72+ ->options (UserRole::class)
4473 ->required ()
45- ->password ()
46- ->dehydrateStateUsing (fn ($ state ) => Hash::make ($ state ))
47- ->visible (fn ($ livewire ) => $ livewire instanceof CreateUser)
48- ->rule (Password::default ()),
49- Forms \Components \TextInput::make ('new_password ' )
50- ->password ()
51- ->label ('New Password ' )
52- ->nullable ()
53- ->rule (Password::default ())
54- ->visible (fn ($ livewire ) => $ livewire instanceof EditUser)
55- ->dehydrated (false ),
56- Forms \Components \TextInput::make ('new_password_confirmation ' )
57- ->password ()
58- ->label ('Confirm New Password ' )
59- ->rule ('required ' , fn ($ get ) => (bool ) $ get ('new_password ' ))
60- ->same ('new_password ' )
61- ->visible (fn ($ livewire ) => $ livewire instanceof EditUser)
62- ->dehydrated (false ),
63- ])
64- ->columns (1 )
65- ->columnSpan ([
66- 'md ' => 2 ,
74+ ->disabled (fn (?User $ record ): bool => Auth::user ()->role !== UserRole::Admin),
75+
76+ // ...
6777 ]),
6878
69- Forms \Components \Grid::make ([
70- 'default ' => 1 ,
71- ])
79+ Forms \Components \Section::make ()
7280 ->schema ([
73- Forms \Components \Section::make ()
74- ->schema ([
75- Forms \Components \Select::make ('role ' )
76- ->options (UserRole::class)
77- ->default (UserRole::User)
78- ->disabled (fn (): bool => ! Auth::user ()->is_admin )
79- ->required (),
80- ])
81- ->columns (1 )
82- ->columnSpan ([
83- 'md ' => 1 ,
84- ]),
85-
86- Forms \Components \Section::make ()
87- ->schema ([
88- Forms \Components \Placeholder::make ('created_at ' )
89- ->content (fn ($ record ) => $ record ?->created_at?->diffForHumans() ?? new HtmlString ('— ' )),
90- Forms \Components \Placeholder::make ('updated_at ' )
91- ->content (fn ($ record ) => $ record ?->updated_at?->diffForHumans() ?? new HtmlString ('— ' )),
92- ])
93- ->columns (1 )
94- ->columnSpan ([
95- 'md ' => 1 ,
96- ]),
97- ])
98- ->columns (1 )
99- ->columnSpan ([
100- 'md ' => 1 ,
81+ Forms \Components \Placeholder::make ('created_at ' )
82+ ->content (fn (?User $ record ): string => $ record ? $ record ->created_at ->diffForHumans () : '- ' ),
83+
84+ Forms \Components \Placeholder::make ('updated_at ' )
85+ ->content (fn (?User $ record ): string => $ record ? $ record ->updated_at ->diffForHumans () : '- ' ),
86+
87+ // ...
10188 ]),
10289 ]),
90+ ])
91+ ->columns ([
92+ 'default ' => 1 ,
93+ 'lg ' => 3 ,
10394 ]);
10495 }
10596
@@ -108,33 +99,45 @@ public static function table(Table $table): Table
10899 return $ table
109100 ->columns ([
110101 Tables \Columns \TextColumn::make ('id ' )
111- ->label ('ID ' ),
102+ ->label ('ID ' )
103+ ->sortable (),
104+
112105 Tables \Columns \TextColumn::make ('name ' )
113106 ->searchable (),
107+
114108 Tables \Columns \TextColumn::make ('email ' )
115109 ->searchable (),
110+
116111 Tables \Columns \TextColumn::make ('role ' )
117112 ->badge (),
113+
118114 Tables \Columns \TextColumn::make ('created_at ' )
119115 ->alignEnd ()
120116 ->dateTime (config ('app.datetime_format ' ))
121- ->timezone (config ('app.display_timezone ' )),
117+ ->timezone (config ('app.display_timezone ' ))
118+ ->sortable ()
119+ ->toggleable (isToggledHiddenByDefault: false ),
120+
122121 Tables \Columns \TextColumn::make ('updated_at ' )
123122 ->alignEnd ()
124123 ->dateTime (config ('app.datetime_format ' ))
125124 ->timezone (config ('app.display_timezone ' ))
125+ ->sortable ()
126126 ->toggleable (isToggledHiddenByDefault: true ),
127+
128+ // ...
127129 ])
128130 ->filters ([
129131 Tables \Filters \SelectFilter::make ('role ' )
130132 ->options (UserRole::class),
131133 ])
132134 ->actions ([
133135 Tables \Actions \ActionGroup::make ([
134- Tables \Actions \ViewAction::make (),
135136 Tables \Actions \EditAction::make (),
136- Tables \Actions \DeleteAction::make (),
137137 ]),
138+ ])
139+ ->bulkActions ([
140+ // ...
138141 ]);
139142 }
140143
0 commit comments