Light_Paladin
Light_Paladin

Reputation: 21

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>

Upvotes: 0

Views: 1078

Answers (4)

Rashed Rahat
Rashed Rahat

Reputation: 2475

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

Upvotes: 1

Jorge Merino
Jorge Merino

Reputation: 123

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.

Upvotes: 0

Nirav Chavda
Nirav Chavda

Reputation: 111

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) { ... }

Upvotes: 0

Abbas
Abbas

Reputation: 23

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')){
    ...
}

Upvotes: 0

Related Questions