TeacherResource.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\TeacherResource\Pages;
  4. use App\Models\Teacher;
  5. use App\Models\User;
  6. use Filament\Forms\Components\TextInput;
  7. use Filament\Forms\Components\Select;
  8. use Filament\Forms\Components\Textarea;
  9. use Filament\Forms\Components\Placeholder;
  10. use Filament\Resources\Resource;
  11. use Filament\Schemas\Schema;
  12. use Filament\Tables;
  13. use Filament\Tables\Table;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Hash;
  16. use Filament\Actions\Action;
  17. use Filament\Actions\BulkAction;
  18. use Filament\Actions\BulkActionGroup;
  19. use Filament\Actions\DeleteAction;
  20. use Filament\Actions\EditAction;
  21. class TeacherResource extends Resource
  22. {
  23. protected static ?string $model = Teacher::class;
  24. protected static bool $shouldRegisterNavigation = false;
  25. public static function form(Schema $schema): Schema
  26. {
  27. return $schema->schema([
  28. // 基本信息字段
  29. TextInput::make('teacher_id')
  30. ->label('教师ID')
  31. ->disabled()
  32. ->columnSpanFull(),
  33. TextInput::make('name')
  34. ->label('教师姓名')
  35. ->required()
  36. ->maxLength(128)
  37. ->placeholder('请输入教师姓名')
  38. ->columnSpanFull()
  39. ->extraAttributes(['style' => 'height: 3rem']),
  40. TextInput::make('subject')
  41. ->label('教授科目')
  42. ->required()
  43. ->maxLength(64)
  44. ->placeholder('例如:数学、语文、英语等')
  45. ->columnSpanFull()
  46. ->extraAttributes(['style' => 'height: 3rem']),
  47. // 登录信息字段
  48. TextInput::make('user.username')
  49. ->label('手机号(登录用户名)')
  50. ->disabled()
  51. ->columnSpanFull()
  52. ->extraAttributes(['style' => 'height: 3rem']),
  53. TextInput::make('user.password_hash')
  54. ->label('密码')
  55. ->password()
  56. ->revealable()
  57. ->dehydrateStateUsing(function ($state) {
  58. return $state ? Hash::make($state) : null;
  59. })
  60. ->placeholder('留空则不修改密码')
  61. ->helperText('留空则不修改原密码')
  62. ->columnSpanFull()
  63. ->extraAttributes(['style' => 'height: 3rem']),
  64. // 备注字段
  65. Textarea::make('remark')
  66. ->label('备注')
  67. ->rows(4)
  68. ->placeholder('请输入备注信息(可选)')
  69. ->columnSpanFull(),
  70. ])->columns(1);
  71. }
  72. public static function table(Table $table): Table
  73. {
  74. return $table
  75. ->columns([
  76. Tables\Columns\TextColumn::make('teacher_id')
  77. ->label('教师ID')
  78. ->badge()
  79. ->color('primary')
  80. ->copyable()
  81. ->copyMessage('教师ID已复制')
  82. ->copyMessageDuration(1500)
  83. ->sortable()
  84. ->searchable(),
  85. Tables\Columns\TextColumn::make('name')
  86. ->label('姓名')
  87. ->weight('bold')
  88. ->searchable()
  89. ->sortable(),
  90. Tables\Columns\TextColumn::make('subject')
  91. ->label('教授科目')
  92. ->badge()
  93. ->color('info')
  94. ->sortable(),
  95. Tables\Columns\TextColumn::make('user.username')
  96. ->label('手机号(登录名)')
  97. ->badge()
  98. ->color('primary')
  99. ->copyable()
  100. ->sortable(),
  101. Tables\Columns\TextColumn::make('user.phone')
  102. ->label('备用手机号')
  103. ->copyable()
  104. ->sortable(),
  105. Tables\Columns\TextColumn::make('user.email')
  106. ->label('邮箱')
  107. ->copyable()
  108. ->sortable()
  109. ->toggleable(isToggledHiddenByDefault: true),
  110. Tables\Columns\TextColumn::make('students_count')
  111. ->label('学生数量')
  112. ->counts('students')
  113. ->sortable()
  114. ->alignCenter()
  115. ->badge()
  116. ->color('success'),
  117. Tables\Columns\TextColumn::make('created_at')
  118. ->label('创建时间')
  119. ->dateTime('Y-m-d H:i')
  120. ->sortable()
  121. ->toggleable(isToggledHiddenByDefault: true),
  122. ])
  123. ->filters([
  124. Tables\Filters\SelectFilter::make('subject')
  125. ->label('教授科目')
  126. ->options(fn () => self::subjectOptions())
  127. ->placeholder('全部科目'),
  128. ])
  129. ->actions([
  130. Action::make('view')
  131. ->label('查看')
  132. ->icon('heroicon-o-eye')
  133. ->url(fn ($record) => static::getUrl('view', ['record' => $record])),
  134. EditAction::make()
  135. ->label('编辑')
  136. ->url(fn ($record) => static::getUrl('edit', ['record' => $record])),
  137. Action::make('delete')
  138. ->label('删除')
  139. ->icon('heroicon-o-trash')
  140. ->color('danger')
  141. ->requiresConfirmation()
  142. ->modalDescription('确定要删除这位教师吗?此操作不可恢复。')
  143. ->action(function ($record) {
  144. $record->delete();
  145. \Filament\Notifications\Notification::make()
  146. ->success()
  147. ->title('删除成功')
  148. ->body("教师 {$record->name} 已被删除")
  149. ->send();
  150. }),
  151. ])
  152. ->bulkActions([
  153. BulkActionGroup::make([
  154. BulkAction::make('deleteBulk')
  155. ->label('批量删除')
  156. ->icon('heroicon-o-trash')
  157. ->color('danger')
  158. ->requiresConfirmation()
  159. ->modalDescription('确定要删除选中的教师吗?此操作不可恢复。')
  160. ->action(function ($records) {
  161. $count = $records->count();
  162. foreach ($records as $record) {
  163. $record->delete();
  164. }
  165. \Filament\Notifications\Notification::make()
  166. ->success()
  167. ->title('批量删除成功')
  168. ->body("已删除 {$count} 位教师")
  169. ->send();
  170. }),
  171. ]),
  172. ])
  173. ->emptyStateHeading('暂无教师记录')
  174. ->emptyStateDescription('开始添加第一位教师吧')
  175. ->emptyStateActions([]);
  176. }
  177. public static function getPages(): array
  178. {
  179. return [
  180. 'index' => Pages\ListTeachers::route('/'),
  181. 'create' => Pages\CreateTeacher::route('/create'),
  182. 'view' => Pages\ViewTeacher::route('/{record}'),
  183. 'edit' => Pages\EditTeacher::route('/{record}/edit'),
  184. ];
  185. }
  186. public static function subjectOptions(): array
  187. {
  188. return [
  189. 'math' => '数学',
  190. 'chinese' => '语文',
  191. 'english' => '英语',
  192. 'physics' => '物理',
  193. 'chemistry' => '化学',
  194. 'biology' => '生物',
  195. 'history' => '历史',
  196. 'geography' => '地理',
  197. 'politics' => '政治',
  198. 'other' => '其他',
  199. ];
  200. }
  201. }