| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace App\Filament\Resources;
- use App\Filament\Resources\StudentResource\Pages;
- use App\Models\Student;
- use App\Models\Teacher;
- use BackedEnum;
- use Filament\Forms\Components\Select;
- use Filament\Forms\Components\Textarea;
- use Filament\Forms\Components\TextInput;
- use Filament\Resources\Resource;
- use Filament\Schemas\Schema;
- use Filament\Tables;
- use Filament\Tables\Table;
- use Illuminate\Support\Facades\Cache;
- class StudentResource extends Resource
- {
- protected static ?string $model = Student::class;
- protected static BackedEnum | string | null $navigationIcon = 'heroicon-o-academic-cap';
- protected static bool $shouldRegisterNavigation = false;
- public static function form(Schema $schema): Schema
- {
- return $schema->schema([
- // 学生ID字段在创建时隐藏,编辑时显示但禁用
- TextInput::make('student_id')
- ->label('学生ID')
- ->disabled()
- ->hidden(fn (?Student $record) => blank($record))
- ->formatStateUsing(fn (?Student $record): string => $record?->student_id ?? ''),
- TextInput::make('name')
- ->label('姓名')
- ->required()
- ->maxLength(128)
- ->placeholder('请输入学生姓名'),
- TextInput::make('grade')
- ->label('年级')
- ->required()
- ->maxLength(32)
- ->placeholder('例如:高一、高二等'),
- TextInput::make('class_name')
- ->label('班级')
- ->helperText('选填项,如不确定可留空')
- ->maxLength(64)
- ->placeholder('例如:1班、2班等'),
- Select::make('teacher_id')
- ->label('指导老师')
- ->options(fn () => self::teacherOptions())
- ->searchable()
- ->required()
- ->preload()
- ->placeholder('请选择指导老师'),
- Textarea::make('remark')
- ->label('备注')
- ->rows(3)
- ->placeholder('请输入备注信息(可选)')
- ->columnSpanFull(),
- ])->columns(2);
- }
- public static function table(Table $table): Table
- {
- return $table
- ->columns([
- Tables\Columns\TextColumn::make('student_id')
- ->label('学生ID')
- ->badge()
- ->color('primary')
- ->copyable()
- ->copyMessage('学生ID已复制')
- ->copyMessageDuration(1500)
- ->sortable()
- ->searchable(),
- Tables\Columns\TextColumn::make('name')
- ->label('姓名')
- ->weight('bold')
- ->searchable()
- ->sortable(),
- Tables\Columns\TextColumn::make('grade')
- ->label('年级')
- ->badge()
- ->color('success')
- ->sortable(),
- Tables\Columns\TextColumn::make('class_name')
- ->label('班级')
- ->placeholder('未分配')
- ->sortable()
- ->formatStateUsing(fn ($state) => $state ?: '未分配'),
- Tables\Columns\TextColumn::make('teacher.user.full_name')
- ->label('指导老师')
- ->default('未分配')
- ->sortable()
- ->searchable(),
- ])
- ->filters([
- Tables\Filters\SelectFilter::make('grade')
- ->label('年级')
- ->options(fn () => self::gradeOptions())
- ->placeholder('全部年级'),
- Tables\Filters\SelectFilter::make('class_name')
- ->label('班级')
- ->options(fn () => self::classOptions())
- ->placeholder('全部班级'),
- Tables\Filters\SelectFilter::make('teacher_id')
- ->label('指导老师')
- ->options(fn () => self::teacherOptions())
- ->placeholder('全部老师'),
- ])
- ->actions([])
- ->bulkActions([])
- ->emptyStateHeading('暂无学生记录')
- ->emptyStateDescription('开始创建你的第一个学生吧')
- ->emptyStateActions([]);
- }
- public static function getPages(): array
- {
- return [
- 'index' => Pages\ListStudents::route('/'),
- 'create' => Pages\CreateStudent::route('/create'),
- 'view' => Pages\ViewStudent::route('/{record}'),
- 'edit' => Pages\EditStudent::route('/{record}/edit'),
- ];
- }
- protected static function teacherOptions(): array
- {
- // 使用缓存优化性能,缓存1小时
- return cache()->remember('teacher_options', 3600, function () {
- return Teacher::with(['user' => function ($query) {
- $query->select('user_id', 'full_name', 'role');
- }])
- ->whereHas('user', function ($query) {
- $query->where('role', 'teacher');
- })
- ->select('teacher_id', 'user_id', 'name')
- ->get()
- ->map(function ($teacher) {
- return [
- 'id' => $teacher->teacher_id,
- 'name' => $teacher->user->full_name ?? $teacher->name
- ];
- })
- ->pluck('name', 'id')
- ->toArray();
- });
- }
- protected static function gradeOptions(): array
- {
- // 使用缓存优化性能,缓存30分钟
- return cache()->remember('grade_options', 1800, function () {
- return Student::query()
- ->select('grade')
- ->distinct()
- ->whereNotNull('grade')
- ->orderBy('grade')
- ->pluck('grade', 'grade')
- ->toArray();
- });
- }
- protected static function classOptions(): array
- {
- // 使用缓存优化性能,缓存30分钟
- return Cache::remember('class_options', 1800, function () {
- return Student::query()
- ->select('class_name')
- ->whereNotNull('class_name')
- ->where('class_name', '!=', '')
- ->distinct()
- ->orderBy('class_name')
- ->pluck('class_name', 'class_name')
- ->toArray();
- });
- }
- /**
- * 清除相关缓存
- */
- public static function clearCaches(): void
- {
- Cache::forget('teacher_options');
- Cache::forget('grade_options');
- Cache::forget('class_options');
- }
- }
|