在 FilamentAdmin 管理后台的 http://fa.test/admin/upload-exam-paper 页面:
grading-panel 组件虽然在父视图中有以下调用:
<livewire:upload-exam.grading-panel
:teacherId="$teacherId"
:studentId="$studentId"
/>
但是没有传递 selectedPaperId 参数,导致组件无法知道要加载哪份试卷的题目。
GradingPanel.php 组件虽然声明了相关属性:
public ?string $teacherId = null;
public ?string $studentId = null;
public ?string $selectedPaperId = null;
但是没有 mount() 方法来接收从父组件传递的参数,因此这些属性始终为 null。
当 selectedPaperId 发生变化时(例如用户切换试卷),组件没有自动重新加载题目数据。
mount() 方法接收参数public function mount(?string $teacherId = null, ?string $studentId = null, ?string $selectedPaperId = null): void
{
$this->teacherId = $teacherId;
$this->studentId = $studentId;
// 如果传入了 selectedPaperId,则直接加载题目
if (!empty($selectedPaperId)) {
$this->selectedPaperId = $selectedPaperId;
$this->loadPaperQuestions();
}
}
updatedSelectedPaperId() 响应式更新public function updatedSelectedPaperId($value): void
{
// 当 selectedPaperId 更新时,自动重新加载题目
if (!empty($value)) {
$this->loadPaperQuestions();
} else {
// 清空数据
$this->questions = [];
$this->gradingData = [];
}
}
loadPaper() 方法签名#[On('loadPaper')]
public function loadPaper(string $paperId, string $teacherId, string $studentId): void
{
$this->selectedPaperId = $paperId;
$this->teacherId = $teacherId;
$this->studentId = $studentId;
$this->loadPaperQuestions();
}
在渲染 grading-panel 组件时传递 selectedPaperId 参数:
{{-- 评分面板组件 --}}
@if(!empty($selectedPaperId))
<livewire:upload-exam.grading-panel
:teacherId="$teacherId"
:studentId="$studentId"
:selectedPaperId="$selectedPaperId"
/>
@endif
用户选择试卷
↓
父组件 selectedPaperId 更新
↓
父组件重新渲染 grading-panel 组件
↓
grading-panel 组件 mount() 方法被调用
↓
接收到 selectedPaperId 参数
↓
调用 loadPaperQuestions() 加载题目
↓
查询 Paper 模型和关联的 PaperQuestion 数据
↓
从题库 API 获取正确答案(如果需要)
↓
设置 $questions 数组
↓
视图渲染题目列表
cd FilamentAdmin
php artisan view:clear
打开浏览器,访问:http://fa.test/admin/upload-exam-paper
通过 tinker 可以验证数据确实存在:
$paper = \App\Models\Paper::where('paper_name', 'like', '%吴%')->latest()->first();
$paper->questions()->count(); // 应该返回题目数量,例如 6
/app/Livewire/UploadExam/GradingPanel.php - 评分面板组件/resources/views/filament/pages/upload-exam-paper.blade.php - 上传试卷页面视图此修复确保了:
修复后,用户可以正常查看和评分已存在的试卷。