Good day everyone!
I have the next issue on my Laravel project
I have a dynamic checkbox that I do it with jquery, my issue is when I try to save on my database, this save me all the values from all the checkbox selected on different lines in one line, I use implode to save in the cell, but save me the same result on all the cells, the input text for "medicamento" and "dosis" working correctly, but the checkbox "cuando1" is not working.
Jquery Script
<script id="document-template" type="text/x-handlebars-template">
<tr class="delete_add_more_item" id="delete_add_more_item">
<td style="width:300px;border: 5px solid transparent"><input type="text" class="form-control" name="medicamentos[]" aria-describedby="medicamentos" id="medicamentos" placeholder="Medicamentos Recetados"></td>
<td style="width:100px;border: 5px solid transparent"><input type="text" class="form-control" name="dosis[]" aria-describedby="dosis" id="dosis" placeholder="Dosis"></td>
<td style="width:50px;border: 5px solid transparent"><label><input type="checkbox" name="cuando[]" id="cuando1" value="Ma?ana"></label></td>
<td style="width:50px;border: 5px solid transparent"><label><input type="checkbox" name="cuando[]" id="cuando2" value="Tarde"></label></td>
<td style="width:50px;border: 5px solid transparent"><label><input type="checkbox" name="cuando[]" id="cuando3" value="Noche"></label></td>
<td>
<button type="button" class="btn btn-danger"><i class="fa fa-minus fa-2x removeaddmore" style="cursor:pointer;color:white;"></i></button>
</td>
</tr>
</script>
<script type="text/javascript">
$(document).on('click','#addMore',function(){
$('.table').show();
var source = $("#document-template").html();
var template = Handlebars.compile(source);
var data = {
medicamentos: medicamentos,
dosis: dosis,
cuando1: cuando1,
cuando2: cuando2,
cuando3: cuando3,
}
var html = template(data);
$("#addRow").append(html)
});
$(document).on('click','.removeaddmore',function(event){
$(this).closest('.delete_add_more_item').remove();
});
</script>
Controller.php
$number = count($request->medicamentos);
for ($i=0; $i < $number; $i++) {
$cuando1 = implode(', ', (array) $request->get('cuando'));
$Medicamentos = new Medicamentos();
$Medicamentos->idpaciente = $idpaciente;
$Medicamentos->medicamentos = $request->medicamentos[$i];
$Medicamentos->fecha = $hoy;
$Medicamentos->dosis = $request->dosis[$i];
$Medicamentos->cuando1 = $cuando1;
$Medicamentos->save();
}
Image of database,check the column "cuando1" with all the values
Image of my blade with the selected checkbox, see that the Selected Checkbox are the saved on all the rows in the database with the implode.
I want to save for aspirina 1: Ma?ana, Tarde, for aspirina 2: Ma?ana and for aspirina 3: Noche
- M = "Ma?ana"
- T = "Tarde"
- N = "Noche"
I appreciate your help
question from:
https://stackoverflow.com/questions/65942479/laravel-how-to-save-on-database-from-dynamic-jquery-checkbox 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…