简体   繁体   English

谁能帮助我如何在laravel中保存多个复选框数据?

[英]Can anyone help me how to save multiple checkbox data in laravel?

I have little information in making a laravel system.我在制作 Laravel 系统方面的信息很少。 My question is how to store the Checked checkbox value into my database.我的问题是如何将 Checked 复选框值存储到我的数据库中。 I can't seem to figure this out.我似乎无法弄清楚这一点。

<div class="controls">
    <td>
        <label class="checkbox inline">
            <input type="checkbox" name="illness" value="cbx_aids">
            Aids
        </label>
    </td>
    <td>
        <label class="checkbox inline">
            <input type="checkbox" name="illness" value="cbx_anemia">
            Anemia
        </label>
    </td>
    <td>
        <label class="checkbox inline">
            <input type="checkbox" name="illness" value="cbx_arthritis">
            Arthritis
        </label>
    </td>
    <td>
        <label class="checkbox inline">
            <input type="checkbox" name="illness" value="cbx_Artificial">
            Artificial Joints
        </label>
    </td>

Just adding name = illness[] instead of name = illness to get all the selected checkbox value.只需添加name = illness[]而不是name = illness即可获取所有选定的复选框值。

<div class="controls">
    <td>
        <label class="checkbox inline">
            <input type="checkbox" name="illness[]" value="cbx_aids">
            Aids
        </label>
    </td>
    <td>
        <label class="checkbox inline">
            <input type="checkbox" name="illness[]" value="cbx_anemia">
            Anemia
        </label>
    </td>
    <td>
        <label class="checkbox inline">
            <input type="checkbox" name="illness[]" value="cbx_arthritis">
            Arthritis
        </label>
    </td>
    <td>
        <label class="checkbox inline">
            <input type="checkbox" name="illness[]" value="cbx_Artificial">
            Artificial Joints
        </label>
    </td>
</div>
public function illNess(Request $request)
    {
        $illness_arr = $request->illness; // returns an array
        if(count($illness_arr) > 0) {
            $new_record = new Illness();
            $new_record->column_name = json_encode($new_record); // pushes as an array into the column of the table
            $new_record->save(); // saves the record into the table
        }
    }

your inputs don't have 'name' attribute您的输入没有“名称”属性

for example:例如:

<label class="checkbox inline">
   <input type="checkbox" name="cbx_aids" value="1">
     Aids
</label>

And in your controller:在您的控制器中:

if($request->has('cbx_aids')){
    ...
}

Maybe I misinterpreted your question but according to my understanding, you want values of all the checked checkboxes.也许我误解了您的问题,但根据我的理解,您需要所有选中复选框的值。 For that, you should give value of name attribute like this to all checkboxes of the same group:为此,您应该为同一组的所有复选框提供这样的 name 属性值:

<input type="checkbox" name="illness[]" value="cbx_aids">

illness[]疾病[]

And in the Controller you can loop through all the values :在控制器中,您可以遍历所有值:

foreach ($request['illness'] as $value) { ... }

I usually make a boolean checkbox adding a jQuery like this:我通常会创建一个布尔复选框,添加这样的 jQuery:

<td>
    <label class="checkbox inline">
        <input id="checkboxId" type="checkbox" name="illness[]" >
        Aids
    </label>
</td>

<script>
    $('#checkboxId').on('change', function(){
        this.value = this.checked ? 1 : 0;
        //alert(this.value);
    }).change();
</script>

Then your request will receive checked input as value 1 and not checked as 0, so you can filter them easily.然后您的请求将接收检查输入作为值 1 而不是检查为 0,因此您可以轻松过滤它们。

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

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