Value in repeater is empty for each item

class TakeQuiz extends Component implements HasSchemas
{
use InteractsWithSchemas;

public array $data = [];
public QuizAttempt $attempt;

public function mount(QuizAttempt $attempt): void
{
$this->attempt = $attempt;

$this->form->fill([]);
}

public function form(Schema $schema): Schema
{
/** @var Quiz $quiz */
$quiz = $this->attempt->quiz;

return $schema
->statePath('data')
->model($quiz)
->columns(1)
->components([
Repeater::make('questions')
->hiddenLabel()
->relationship()
->itemLabel(fn (array $state): ?string => $state['title'] ?? 'Nieuwe vraag')
->schema([
Radio::make('answers')
->hiddenLabel()
->required()
->options(fn (QuizQuestion $record) => $record->options->pluck('option_text', 'id')->toArray())
->visible(fn (QuizQuestion $record) => $record->type === QuizQuestionType::SINGLE_CHOICE),
CheckboxList::make('answers')
->hiddenLabel()
->minItems(1)
->options(fn (QuizQuestion $record) => $record->options->pluck('option_text', 'id')->toArray())
->visible(fn (QuizQuestion $record) => $record->type === QuizQuestionType::MULTIPLE_CHOICE),
])
->orderColumn(false)
->deletable(false)
->addable(false)
]);
}

public function create(): void
{
$this->form->getState();

$data = $this->data;

dd($data);
}
}
class TakeQuiz extends Component implements HasSchemas
{
use InteractsWithSchemas;

public array $data = [];
public QuizAttempt $attempt;

public function mount(QuizAttempt $attempt): void
{
$this->attempt = $attempt;

$this->form->fill([]);
}

public function form(Schema $schema): Schema
{
/** @var Quiz $quiz */
$quiz = $this->attempt->quiz;

return $schema
->statePath('data')
->model($quiz)
->columns(1)
->components([
Repeater::make('questions')
->hiddenLabel()
->relationship()
->itemLabel(fn (array $state): ?string => $state['title'] ?? 'Nieuwe vraag')
->schema([
Radio::make('answers')
->hiddenLabel()
->required()
->options(fn (QuizQuestion $record) => $record->options->pluck('option_text', 'id')->toArray())
->visible(fn (QuizQuestion $record) => $record->type === QuizQuestionType::SINGLE_CHOICE),
CheckboxList::make('answers')
->hiddenLabel()
->minItems(1)
->options(fn (QuizQuestion $record) => $record->options->pluck('option_text', 'id')->toArray())
->visible(fn (QuizQuestion $record) => $record->type === QuizQuestionType::MULTIPLE_CHOICE),
])
->orderColumn(false)
->deletable(false)
->addable(false)
]);
}

public function create(): void
{
$this->form->getState();

$data = $this->data;

dd($data);
}
}
Can someone explain to me why I get 'answers' => [] for all my questions? I got this working earlier, but now I do not understand what I broke.
2 Replies
Merdin
MerdinOP2w ago
dd() output:
array:1 [▼ // app/Livewire/TakeQuiz.php:73
"questions" => array:3 [▼
"record-1" => array:8 [▼
"id" => 1
"quiz_id" => 1
"title" => "Magnam et qui ab temporibus."
"type" => "multiple_choice"
"points" => 1.0
"created_at" => "2025-11-30T23:09:43.000000Z"
"updated_at" => "2025-11-30T23:09:43.000000Z"
"answers" => []
]
"record-2" => array:8 [▼
"id" => 2
"quiz_id" => 1
"title" => "Assumenda excepturi velit ex quia tempore."
"type" => "multiple_choice"
"points" => 1.0
"created_at" => "2025-11-30T23:09:43.000000Z"
"updated_at" => "2025-11-30T23:09:43.000000Z"
"answers" => []
]
"record-3" => array:8 [▼
"id" => 3
"quiz_id" => 1
"title" => "Aliquid omnis fuga repellendus quibusdam eaque repellendus similique."
"type" => "single_choice"
"points" => 0.5
"created_at" => "2025-11-30T23:09:43.000000Z"
"updated_at" => "2025-11-30T23:09:43.000000Z"
"answers" => []
]
]
]
array:1 [▼ // app/Livewire/TakeQuiz.php:73
"questions" => array:3 [▼
"record-1" => array:8 [▼
"id" => 1
"quiz_id" => 1
"title" => "Magnam et qui ab temporibus."
"type" => "multiple_choice"
"points" => 1.0
"created_at" => "2025-11-30T23:09:43.000000Z"
"updated_at" => "2025-11-30T23:09:43.000000Z"
"answers" => []
]
"record-2" => array:8 [▼
"id" => 2
"quiz_id" => 1
"title" => "Assumenda excepturi velit ex quia tempore."
"type" => "multiple_choice"
"points" => 1.0
"created_at" => "2025-11-30T23:09:43.000000Z"
"updated_at" => "2025-11-30T23:09:43.000000Z"
"answers" => []
]
"record-3" => array:8 [▼
"id" => 3
"quiz_id" => 1
"title" => "Aliquid omnis fuga repellendus quibusdam eaque repellendus similique."
"type" => "single_choice"
"points" => 0.5
"created_at" => "2025-11-30T23:09:43.000000Z"
"updated_at" => "2025-11-30T23:09:43.000000Z"
"answers" => []
]
]
]
dd $this->form->getState()) also returns an empty array
toeknee
toeknee6d ago
try: radio_answers checkbox_answers And try getRawState since getState is not usually inclusive of relationships

Did you find this page helpful?