Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kmnurunnabi's avatar

How to use Multiple Livewire form

Hey. I have a livewire component with few forms is given below:

<?php

namespace App\Livewire\User\Quizzes;

use App\Livewire\Forms\User\AnswerForm;
use App\Livewire\Forms\User\OptionForm;
use App\Livewire\Forms\User\QuestionForm;
use App\Livewire\Forms\User\QuizForm;
use App\Models\Quiz;
use Livewire\Component;

class CreateQuiz extends Component
{
    public QuizForm $quizForm;

    public QuestionForm $questionForm;

    public function save()
    {
        dd($this->questionForm->all());
        $this->validate();
        Quiz::create($this->quizForm->all());
    }

    public function render()
    {
        return view('livewire.user.quizzes.create-quiz');
    }
}

But question is, quiz can have mutiple questions. That mean I need a collection of QuestionForm class. But I don't know how can I acquired that. Does anybody knows how to take an array of QuestionForm class?

0 likes
1 reply
ramonrietdijk's avatar

If you wish to add multiple questions to a quiz, wouldn't it be easier to add an array property to the QuizForm containing them?

Despite the data is nested, they can still be easily validated.

public $questions;

public function rules()
{
    return [
        'questions' => 'array',
        'questions.*.question' => 'required|string',
        'questions.*.answer' => 'required|string',
    ];
}

Please or to participate in this conversation.