I had a table form where I will add multiple users in table and it has checkboxes. My problem is on the checkboxes.
User 1: ✓ Add, ✓ Edit, ✓ Delete
User 2: ✓ Add, Edit, Delete
Expected Output in Database
Full Name | Permissions
John Doe | ["Add", "Edit", "Delete"]
Jane Doe | ["Add"]
What is inserted in the DB
Full Name | Permissions
John Doe | "Add"
Jane Doe | "Edit"
blade html
<button type="button" id="addUser">Add</button>
<table id="userTable">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Add</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
JS Code
$("#addUser").click(function () {
t.row.add(['',
'<input class="form-control" type="text" name="fullname[]">',
'<input class="form-control" type="checkbox" name="permissions[]" value="Add" data-parsley-multiple="status">',
'<input class="form-control" type="checkbox" name="permissions[]" value="Edit" data-parsley-multiple="status">',
'<input class="form-control" type="checkbox" name="permissions[]" value="Delete" data-parsley-multiple="status">',
]).draw(false);
});
Controller
public function store(Request $request)
{
foreach($request->fullname as $key=>$insert)
{
$user = [
'fullname' => $request->fullname[$key],
'permissions' => json_encode($request->permissions[$key]),
];
DB::table('users')->insert($user);
}
}
In my code, $request->permissions[$key] is only getting 1 value per permission. How can I get all the checked value of permission in one user row and to be inserted for that user.
name="permissions[' + index +']"createvar indexevery time you add a user, and make sure it is uniquefullname[2]they are 4,5,6 etc.