Hello all, I'm trying to update multi records of my quiz. I'm new in this so i really don't know what is the problem with my controller.
`public function update(Request $request) { $quizId = $request->get('quizId'); $title = $request->get('title'); $body = $request->get('body'); $questionId = $request->get('questionId'); $question = $request->get('question'); $answer = $request->get('answer'); $correct = $request->get('correct');
if (request()->hasFile('image')) {
$filenameWithExt = request()->file('image')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = request()->file('image')->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . time() . '.' . $extension;
request()->image->move(public_path('/quizzes'), $fileNameToStore);
}else{
}
$quizData = Quizze::where('id', $quizId)->update([
'title' => $title,
'image' => $fileNameToStore,
'body' => $body
]);
foreach($question as $q) {
$questionData = QuizQuestion::where('quizze_id', $quizId)->update([
'question' => $q
]);
}
foreach($answer as $a) {
$answerData = QuizAnswer::where('question_id', $questionId)->update([
'answer' => $a,
'correct' => $correct
]);
}
return redirect('admin/quizzes')->with('success', 'success message');
response()->json([
"update" => $quizData, $questionData, $answerData
]);
}`
my blade
`<form action="/admin/quiz/update" method="POST" enctype="multipart/form-data"> <div hidden name="quizId" value="{{ $quiz->id }}"> </div> @csrf <div class="form-group"> ქვიზის სახელი <input name="title" type="text" class="form-control" value="{{ $quiz->title }}" id="" placeholder=""> @error('title.title') <small class="text-danger">{{ $message }}</small> @enderror </div>
<div class="form-group">
<div class="col-md-6">
<input type="file" class="form-control-file" name="image" accept="image/*">
</div>
</div>
@foreach($questions as $question)
<div class="form-group">
ქვიზის შეკითხვა
<div name="questionId" value="{{ $question->id }}">
<input name="question[]" type="text" class="form-control" value="{{ $question->question }}" id="" placeholder="">
</div>
@error('title.title')
<small class="text-danger">{{ $message }}</small>
@enderror
</div>
@foreach($question->answers as $answer)
<div class="form-group">
ქვიზის პასუხი
<input type="checkbox" value="1" name="correct[]" @if($answer->correct !== 0) class="active form-check-input" checked @else class="form-check-input" @endif>
<input name="answer[]" type="text" class="form-control" value="{{ $answer->answer }}" id="" placeholder="">
@error('title.title')
<small class="text-danger">{{ $message }}</small>
@enderror
</div>
@endforeach
@endforeach
<button type="submit" class="btn btn-primary">ცვლილებების შენახვა</button>
</form>`
Hey!
You are using div
tags for form elements like: <div hidden name="quizId" value="{{ $quiz->id }}">
or <div name="questionId" value="{{ $question->id }}">
which is not a valid form input/element.
Your quizId
and questionId
fields are empty in the request as they don't exist because of how you use them.
If you do it like <input type="hidden" name="quizId" value="{{ $quiz->id }}"/>
and <input type="hidden" name="questionId" value="{{ $question->id }}" />
it should send both IDs to the controller and update your items accordingly.
thanks for answer but it's still don't works other code of mine is correct?
Hey!
What doesn't work. Did you get an error? Can you please post your code again and include it in 3 backticks ```
instead of one to make it easier readable.
Thanks!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community