Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
Insert unchecked checkbox value to database in Laravel 8

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).

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

If I got you right, you want to get both checked and unchecked checkbox values which means you want to get all input values in the form. Then why you need checkboxes actually? They are used for filtering data which means u may need only checked or unchecked values in ideal world. If you still want to get all checkbox values for some reason you can use additional hidden checkbox inputs like

<input id="score_evaluation_id-{{ $scoreEvaluation->id }}"
type="hidden" value="{{ $scoreEvaluation->marks }}"
name='score_evaluation_id-{{ $scoreEvaluation->id }}'>

<input id="score_evaluation_id-{{ $scoreEvaluation->id }}" 
type="checkbox" value="{{ $scoreEvaluation->marks }}" 
name="score_evaluation_id-{{ $scoreEvaluation->id }}">

With that logic you will get your desired value in backend whether checkbox is checked or not.

But if you want to get only unchecked values, then check out this issue - POST unchecked HTML checkboxes

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!