I wrote code for an evaluation form related to quality management. I have a portion in which multiple checkboxes are set to be checked in the code. The database dynamically generates all the names, values, and ids.
@foreach ($scoreEvaluations as $scoreEvaluation)
<div class="col-sm-2">
<!-- radio -->
<div class="form-group">
<div class="form-check">
<input class="form-check-input score" type="checkbox"
name="score_evaluation_id-{{ $scoreEvaluation->id }}"
value="{{ $scoreEvaluation->marks }}"
id="score_evaluation_id-{{ $scoreEvaluation->id }}"
onclick="score_check(this)"
checked>
<label class="form-check-label">
{{ $scoreEvaluation->name }}
</label>
</div>
</div>
</div>
@endforeach
From the above code, the form is submitted when I submit the form, but it only saves the value of the checked checkboxes. At the same time, I want to store the values of checked as well as unchecked checkboxes.
Controller
public function store(QCFeedbackRequest $request, QCFeedback $feedback)
{
$feedback = $feedback->create($request->all());
$this->addScoreResponse($request, $feedback);
Session::flash('success', 'Feedback added successfully!');
return redirect()->route('qc-feedback-audits.index');
}
public function addScoreResponse($request, $feedback)
{
foreach ($request->all() as $key => $value) {
$key_arr = explode('-', $key);
if ($key_arr[0] == 'score_evaluation_id') {
$score_response = new ScoreEvaluationResponse;
$score_response->score_evaluation_id = $key_arr[1];
$score_response->qc_feedback_id = $feedback->id;
$score_response->checkbox_status = 'Checked';
$score_response->save();
}
}
}
The function store()
is adding the code in the qc_feedbacks
table, and the other function is to add the responses of the checkboxes in the table of score_evaluation_responses(having fields id, qc_feedback_id (foreign key of qc_feedbacks) and score_evaluation_id (another foreign key for score_evaluations table).