StudentResource.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\StudentResource\Pages;
  4. use App\Models\Student;
  5. use App\Models\Teacher;
  6. use BackedEnum;
  7. use Filament\Forms\Components\Select;
  8. use Filament\Forms\Components\Textarea;
  9. use Filament\Forms\Components\TextInput;
  10. use Filament\Resources\Resource;
  11. use Filament\Schemas\Schema;
  12. use Filament\Tables;
  13. use Filament\Tables\Table;
  14. use Illuminate\Support\Facades\Cache;
  15. class StudentResource extends Resource
  16. {
  17. protected static ?string $model = Student::class;
  18. protected static BackedEnum | string | null $navigationIcon = 'heroicon-o-academic-cap';
  19. protected static bool $shouldRegisterNavigation = false;
  20. public static function form(Schema $schema): Schema
  21. {
  22. return $schema->schema([
  23. // 学生ID字段在创建时隐藏,编辑时显示但禁用
  24. TextInput::make('student_id')
  25. ->label('学生ID')
  26. ->disabled()
  27. ->hidden(fn (?Student $record) => blank($record))
  28. ->formatStateUsing(fn (?Student $record): string => $record?->student_id ?? ''),
  29. TextInput::make('name')
  30. ->label('姓名')
  31. ->required()
  32. ->maxLength(128)
  33. ->placeholder('请输入学生姓名'),
  34. TextInput::make('grade')
  35. ->label('年级')
  36. ->required()
  37. ->maxLength(32)
  38. ->placeholder('例如:高一、高二等'),
  39. TextInput::make('class_name')
  40. ->label('班级')
  41. ->helperText('选填项,如不确定可留空')
  42. ->maxLength(64)
  43. ->placeholder('例如:1班、2班等'),
  44. Select::make('teacher_id')
  45. ->label('指导老师')
  46. ->options(fn () => self::teacherOptions())
  47. ->searchable()
  48. ->required()
  49. ->preload()
  50. ->placeholder('请选择指导老师'),
  51. Textarea::make('remark')
  52. ->label('备注')
  53. ->rows(3)
  54. ->placeholder('请输入备注信息(可选)')
  55. ->columnSpanFull(),
  56. ])->columns(2);
  57. }
  58. public static function table(Table $table): Table
  59. {
  60. return $table
  61. ->columns([
  62. Tables\Columns\TextColumn::make('student_id')
  63. ->label('学生ID')
  64. ->badge()
  65. ->color('primary')
  66. ->copyable()
  67. ->copyMessage('学生ID已复制')
  68. ->copyMessageDuration(1500)
  69. ->sortable()
  70. ->searchable(),
  71. Tables\Columns\TextColumn::make('name')
  72. ->label('姓名')
  73. ->weight('bold')
  74. ->searchable()
  75. ->sortable(),
  76. Tables\Columns\TextColumn::make('grade')
  77. ->label('年级')
  78. ->badge()
  79. ->color('success')
  80. ->sortable(),
  81. Tables\Columns\TextColumn::make('class_name')
  82. ->label('班级')
  83. ->placeholder('未分配')
  84. ->sortable()
  85. ->formatStateUsing(fn ($state) => $state ?: '未分配'),
  86. Tables\Columns\TextColumn::make('teacher.user.full_name')
  87. ->label('指导老师')
  88. ->default('未分配')
  89. ->sortable()
  90. ->searchable(),
  91. ])
  92. ->filters([
  93. Tables\Filters\SelectFilter::make('grade')
  94. ->label('年级')
  95. ->options(fn () => self::gradeOptions())
  96. ->placeholder('全部年级'),
  97. Tables\Filters\SelectFilter::make('class_name')
  98. ->label('班级')
  99. ->options(fn () => self::classOptions())
  100. ->placeholder('全部班级'),
  101. Tables\Filters\SelectFilter::make('teacher_id')
  102. ->label('指导老师')
  103. ->options(fn () => self::teacherOptions())
  104. ->placeholder('全部老师'),
  105. ])
  106. ->actions([])
  107. ->bulkActions([])
  108. ->emptyStateHeading('暂无学生记录')
  109. ->emptyStateDescription('开始创建你的第一个学生吧')
  110. ->emptyStateActions([]);
  111. }
  112. public static function getPages(): array
  113. {
  114. return [
  115. 'index' => Pages\ListStudents::route('/'),
  116. 'create' => Pages\CreateStudent::route('/create'),
  117. 'view' => Pages\ViewStudent::route('/{record}'),
  118. 'edit' => Pages\EditStudent::route('/{record}/edit'),
  119. ];
  120. }
  121. protected static function teacherOptions(): array
  122. {
  123. // 使用缓存优化性能,缓存1小时
  124. return cache()->remember('teacher_options', 3600, function () {
  125. return Teacher::with(['user' => function ($query) {
  126. $query->select('user_id', 'full_name', 'role');
  127. }])
  128. ->whereHas('user', function ($query) {
  129. $query->where('role', 'teacher');
  130. })
  131. ->select('teacher_id', 'user_id', 'name')
  132. ->get()
  133. ->map(function ($teacher) {
  134. return [
  135. 'id' => $teacher->teacher_id,
  136. 'name' => $teacher->user->full_name ?? $teacher->name
  137. ];
  138. })
  139. ->pluck('name', 'id')
  140. ->toArray();
  141. });
  142. }
  143. protected static function gradeOptions(): array
  144. {
  145. // 使用缓存优化性能,缓存30分钟
  146. return cache()->remember('grade_options', 1800, function () {
  147. return Student::query()
  148. ->select('grade')
  149. ->distinct()
  150. ->whereNotNull('grade')
  151. ->orderBy('grade')
  152. ->pluck('grade', 'grade')
  153. ->toArray();
  154. });
  155. }
  156. protected static function classOptions(): array
  157. {
  158. // 使用缓存优化性能,缓存30分钟
  159. return Cache::remember('class_options', 1800, function () {
  160. return Student::query()
  161. ->select('class_name')
  162. ->whereNotNull('class_name')
  163. ->where('class_name', '!=', '')
  164. ->distinct()
  165. ->orderBy('class_name')
  166. ->pluck('class_name', 'class_name')
  167. ->toArray();
  168. });
  169. }
  170. /**
  171. * 清除相关缓存
  172. */
  173. public static function clearCaches(): void
  174. {
  175. Cache::forget('teacher_options');
  176. Cache::forget('grade_options');
  177. Cache::forget('class_options');
  178. }
  179. }