简体   繁体   English

Laravel:如何从动态 jquery 复选框保存到数据库

[英]Laravel: How to save on database from dynamic jquery checkbox

Good day everyone!今天是个好日子!

I have the next issue on my Laravel project我的 Laravel 项目有下一个问题

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 执行此操作,我的问题是当我尝试保存在我的数据库中时,这将我保存在一行中不同行上选择的所有复选框中的所有值,我使用 implode 保存在单元格中,但在所有单元格上保存相同的结果,“medicamento”和“dosis”的输入文本正常工作,但复选框“cuando1”不起作用。

Jquery Script Jquery 脚本

<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 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数据库图像,检查列“cuando1”的所有值

具有“cuando”列的数据库

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我想为 aspirina 1:Mañana、Tarde、为 aspirina 2:Mañana 和为 aspirina 3:Noche 存钱

  • M = "Mañana" M =“玛娜娜”
  • T = "Tarde" T =“迟到”
  • N = "Noche" N =“诺切”

在此处输入图像描述

I appreciate your help我感谢您的帮助

It's because your getting all of the values of checkboxes on line below:这是因为您在下面的行中获取了所有复选框的值:

$cuando1 = implode(', ', (array) $request->get('cuando'));

In your html file, instead of cuando[] use cuando[i][] where i starts from zero and gets incremented with each loop of template.在您的 html 文件中,使用cuando[i][]而不是cuando[] ] ,其中i从零开始,并随着模板的每个循环而递增。

Then in your controller use:然后在您的 controller 中使用:

$cuando1 = implode(', ', (array) $request->cuando[$i]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM