Selam.
$question = $questionnaire->questions()->create($data['question']);
$question->answers()->createMany($data);
bu kodu
public function questionAdd(QuestionRequest $request) {
$data = $request->validated();
$questionId = DB::table('questions')->insertGetId(['question' => $data['question']['question']]);
$answers = [];
foreach ($data['answers'] as $answer) {
$answers[] = ['question_id' => $questionId, 'answer' => $answer['answer']];
};
DB::table('answers')->insert($answers);
return redirect()->route('questionnaireIndex');
}
public function rules(): array
{
return [
'question.question' => 'required',
'answers.*.answer' => 'required',
];
}
bu şekilde yazdım fakat bana
The question.question field is required.
hatası döndürdü
public function rules(): array
{
return [
'question.*' => 'required',
'answers.*.answer' => 'required',
];
}
bu şekilde yaptığımda ise
Undefined array key "question"
<form action="{{route('questionAdd', $questionnaire->id)}}" method="POST" enctype="multipart/form-data">
@csrf
<div class="card-text h-full my-6 space-y-4">
<div class="input-area">
<label for="question" class="form-label">Question</label>
<input id="question" type="text" name="question[]" class="form-control" placeholder="Question" value="{{ old('question.0') }}" />
</div>
</div>
<div class="form-group mb-5">
<fieldset>
<legend>Choices</legend>
<small id="choicesHelp" class="form-text text-muted">Give choices question</small>
<div class="mt-3">
<div class="form-group">
<label for="answer1">Choice 1</label>
<input type="text" name="answers[][answer]" class="form-control" id="answer1" aria-describedby="choicesHelp" placeholder="Enter Choice 1" value="{{ old('answers.0.answer') }}" />
</div>
</div>
<div class="mt-3">
<div class="form-group">
<label for="answer2">Choice 2</label>
<input type="text" name="answers[][answer]" class="form-control" id="answer2" aria-describedby="choicesHelp" placeholder="Enter Choice 2" value="{{ old('answers.1.answer') }}" />
</div>
</div>
<div class="mt-3">
<div class="form-group">
<label for="answer3">Choice 3</label>
<input type="text" name="answers[][answer]" class="form-control" id="answer3" aria-describedby="choicesHelp" placeholder="Enter Choice 3" value="{{ old('answers.2.answer') }}" />
</div>
</div>
<div class="mt-3">
<div class="form-group">
<label for="answer4">Choice 4</label>
<input type="text" name="answers[][answer]" class="form-control" id="answer4" aria-describedby="choicesHelp" placeholder="Enter Choice 4" value="{{ old('answers.3.answer') }}" />
</div>
</div>
<div class="mt-3">
<div class="form-group">
<label for="answer5">Choice 5</label>
<input type="text" name="answers[][answer]" class="form-control" id="answer5" aria-describedby="choicesHelp" placeholder="Enter Choice 5" value="{{ old('answers.4.answer') }}" />
</div>
</div>
</fieldset>
</div>
</div>
<button type="submit" class="btn inline-flex justify-center btn-outline-info">Create Question</button>
</div>
</form>
bunu nasıl çözebilirim.