Reputation: 45
I have course and course is having questions and answers. So i'm trying to store course_id questions, answers and correct answer. questions, answers and correct answers stores. I don't have problem with it but i can't store course_id on course_id it's stores NULL.
$data = request()->validate([
'questions.*.course_id' => '',
'questions.*.question' => 'required',
'questions.*.answers.*.answer' => 'required',
'questions.*.answers.*.correct' => '',
], [
'questions.*.course_id.required' => '',
'questions.*.question.required' => '',
'questions.*.answers.*.answer.required' => ''
]);
foreach ($data['questions'] as $key => $q) {
$question = $CourseQuestion->create(['question' => $q['question']]);
$question->answers()->createMany($data['questions'][$key]['answers']);
}
return redirect()->back();
In blade
<input name="questions[1][question]" type="text" class="form-control" value="{{ old('questions.0.question') }}">
<input type="text" value="{{ $c->id }}" name="questions[1][course_id]">
<input type="checkbox" value="1" name="questions[1][answers][1][correct]">
<input name="questions[1][answers][1][answer]" type="text" class="form-control" value="{{ old('questions.1.answers.0.answer') }}" >
Whats i'm doing wrong?
Upvotes: 0
Views: 43
Reputation: 45
Problem was that i was not passing course_id. So i changed my store function
public function store(CourseQuestion $CourseQuestion)
{
$course = Course::find(request()->get('courseId'));
$data = request()->validate([
'questions.*.question' => 'required',
'questions.*.answers.*.answer' => 'required',
'questions.*.answers.*.correct' => '',
], [
'questions.*.question.required' => '',
'questions.*.answers.*.answer.required' => ''
]);
foreach ($data['questions'] as $key => $q) {
$question = $course->questions()->create(['question' => $q['question']]);
$question->answers()->createMany($data['questions'][$key]['answers']);
}
return redirect()->back();
}
I'm getting now current course and then with that course adding questions and then answers : )
Thanks all for response <3
Upvotes: 1