class Exam extends Page implements HasForms
{
use InteractsWithForms;
protected static ?string $navigationIcon = 'heroicon-o-document-text';
protected static string $view = 'filament.user.pages.exam';
public ?array $data = [];
public function form(Form $form): Form
{
return $form
->schema(self::getQuestions())
->statePath('data');
}
private static function getQuestions(): array
{
$qs = \App\Models\Exam::get();
$questions = [];
foreach ($qs as $question) {
$questions[] =
Radio::make('question_' . $question->id)
->label(function () use ($question) {
return $question->question;
})
->options(function () use ($question) {
$options = [];
foreach ($question->answers as $key => $answer) {
$options[$key] = $answer;
}
return $options;
})
->required();
}
return $questions;
}
public function submit()
{
$this->form->getState();
}
public function mount()
{
$this->form->fill();
}
}