i'm having difficulty saving multiple item using checkbox, with this code i can only save one value at a time..can anyone give me idea
Controller:
$crud = explode(',', $request->crud_selected);
if (count($crud) > 0) {
foreach ($crud as $x) {
$slug = strtolower($x) . '-' . strtolower($request->resource);
$display_name = ucwords($x . " " . $request->resource);
$description = "Allows a user to " . strtoupper($x) . ' a ' . ucwords($request->resource);
$permission = new Permission();
$permission->name = $slug;
$permission->display_name = $display_name;
$permission->description = $description;
$permission->save();
}
Session::flash('success', 'Permissions were all successfully added');
return redirect()->route('permissions.index');
}
} else {
return redirect()->route('permissions.create')->withInput();
}
View-blade:
<script>
var app = new Vue({
el: '#app',
data() {
return:{
permissionType: 'basic',
resource: '',
crudSelected: []
}
}
});
</script>
<div class="from-group" v-if="permissionType == 'crud'">
<div class="checkbox-group" v-model="crudSelected">
<label class="checkbox-inline"><input type="checkbox" name="crud_selected" value="create">Create</label>
<label class="checkbox-inline"><input type="checkbox" name="crud_selected" value="read">Read</label>
<label class="checkbox-inline"><input type="checkbox" name="crud_selected" value="message">Update</label>
<label class="checkbox-inline"><input type="checkbox" name="crud_selected" value="message">Delete</label>
</div>
</div>
Any idea how to fix this.
</div>
I already manage to find answer this is what do i change the controller code.
Old
$crud = explode(',', $request->crud_selected);
New
$crud = $request->input('crud_selected');
then on my view-blade i add this to my
name="crud_selected[]
Wallahh it works..
you not need explode value because the checkbox values return array. v-model only use in input.
<div class="from-group" v-if="permissionType == 'crud'">
<div class="checkbox-group" >
<label class="checkbox-inline"><input type="checkbox" v-model="crudSelected">Create</label>
<label class="checkbox-inline"><input type="checkbox" v-model="crudSelected">Read</label>
<label class="checkbox-inline"><input type="checkbox" v-model="crudSelected">Update</label>
<label class="checkbox-inline"><input type="checkbox" v-model="crudSelected">Delete</label>
</div>
</div>
if you want to take more than checked value on checkbox inputs, i think you can do it by define a function will call on each change event on your check box for example like this
<label class="checkbox-inline"><input type="checkbox" id ="checklval" name="crud_selected" v-model="crudSelected" v-on:change="trackcheck(Create)" :value="Create" >Create</label>
then on your vue component script you will implement this function, i define array called ValArray will pushed on it each value selected on check box and will also pop from it if this value not checked on check box like this below :
trackcheck($labid) {
if(document.getElementById('checklval').checked==true){
this.ValArray.push($labid)
} else if(document.getElementById('checklval' ).checked==false){
this.ValArray.pop($labid)
}
}
now you will have an array with each selected value on check box inputs, hope this help you