StudentResource.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\StudentResource\Pages;
  4. use App\Models\Student;
  5. use BackedEnum;
  6. use Filament\Forms\Components\Select;
  7. use Filament\Forms\Components\Textarea;
  8. use Filament\Forms\Components\TextInput;
  9. use Filament\Resources\Resource;
  10. use Filament\Schemas\Schema;
  11. use Filament\Tables;
  12. use Filament\Tables\Table;
  13. use Illuminate\Support\Facades\DB;
  14. class StudentResource extends Resource
  15. {
  16. protected static ?string $model = Student::class;
  17. protected static BackedEnum | string | null $navigationIcon = 'heroicon-o-academic-cap';
  18. protected static bool $shouldRegisterNavigation = false;
  19. public static function form(Schema $schema): Schema
  20. {
  21. return $schema->components([
  22. TextInput::make('student_id')
  23. ->label('学生ID')
  24. ->numeric()
  25. ->required()
  26. ->disabled(fn (?Student $record) => filled($record)),
  27. TextInput::make('name')
  28. ->label('姓名')
  29. ->required()
  30. ->maxLength(128),
  31. TextInput::make('grade')
  32. ->label('年级')
  33. ->required()
  34. ->maxLength(32),
  35. TextInput::make('class_name')
  36. ->label('班级')
  37. ->required()
  38. ->maxLength(64),
  39. Select::make('teacher_id')
  40. ->label('指导老师')
  41. ->options(fn () => self::teacherOptions())
  42. ->searchable()
  43. ->required()
  44. ->preload(),
  45. Textarea::make('remark')
  46. ->label('备注')
  47. ->rows(3)
  48. ->columnSpanFull(),
  49. ])->columns(2);
  50. }
  51. public static function table(Table $table): Table
  52. {
  53. return $table
  54. ->columns([
  55. Tables\Columns\TextColumn::make('student_id')
  56. ->label('学生ID')
  57. ->sortable()
  58. ->searchable(),
  59. Tables\Columns\TextColumn::make('name')
  60. ->label('姓名')
  61. ->weight('bold')
  62. ->searchable(),
  63. Tables\Columns\TextColumn::make('grade')
  64. ->label('年级')
  65. ->sortable(),
  66. Tables\Columns\TextColumn::make('class_name')
  67. ->label('班级')
  68. ->sortable(),
  69. ])
  70. ->filters([
  71. Tables\Filters\SelectFilter::make('grade')
  72. ->label('年级')
  73. ->options(fn () => self::gradeOptions()),
  74. Tables\Filters\SelectFilter::make('class_name')
  75. ->label('班级')
  76. ->options(fn () => self::classOptions()),
  77. ]);
  78. }
  79. public static function getPages(): array
  80. {
  81. return [
  82. 'index' => Pages\ListStudents::route('/'),
  83. 'create' => Pages\CreateStudent::route('/create'),
  84. 'view' => Pages\ViewStudent::route('/{record}'),
  85. 'edit' => Pages\EditStudent::route('/{record}/edit'),
  86. ];
  87. }
  88. protected static function teacherOptions(): array
  89. {
  90. return DB::table('teachers')
  91. ->join('users', 'teachers.user_id', '=', 'users.user_id')
  92. ->where('users.role', 'teacher')
  93. ->pluck('users.full_name', 'teachers.teacher_id')
  94. ->toArray();
  95. }
  96. protected static function gradeOptions(): array
  97. {
  98. return DB::table('students')
  99. ->distinct()
  100. ->orderBy('grade')
  101. ->pluck('grade', 'grade')
  102. ->toArray();
  103. }
  104. protected static function classOptions(): array
  105. {
  106. return DB::table('students')
  107. ->distinct()
  108. ->orderBy('class_name')
  109. ->pluck('class_name', 'class_name')
  110. ->toArray();
  111. }
  112. }