Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

265
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda