简体   繁体   中英

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

I have little information in making a laravel system. My question is how to store the Checked checkbox value into my database. 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.

<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:

<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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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