0

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.

2
  • Add a unique index for every user in your input name attibute name="permissions[' + index +']" create var index every time you add a user, and make sure it is unique Commented Jul 28, 2022 at 6:11
  • Because for one fullname you are getting more than one permission. For example if your $key is 1 your permissions are not stored in 1 index. They are like 1, 2, 3. For fullname[2] they are 4,5,6 etc. Commented Jul 28, 2022 at 6:13

1 Answer 1

1

Add indexes to your input names, so they are grouped per user:

    $("#addUser").click(function () {
    var index = $(".user-name-field").length;
    t.row.add(['', 
        '<input class="form-control user-name-field" type="text" name="fullname[' + index + ']">',
        '<input class="form-control" type="checkbox" name="permissions[' + index + '][]" value="Add" data-parsley-multiple="status">',
        '<input class="form-control" type="checkbox" name="permissions[' + index + '][]" value="Edit" data-parsley-multiple="status">',
        '<input class="form-control" type="checkbox" name="permissions[' + index + '][]" value="Delete" data-parsley-multiple="status">',
    ]).draw(false); 
});

In this case i've added a class to the user name field, count these fields to create the index

Sign up to request clarification or add additional context in comments.

7 Comments

@VüsalHüseynli this way the key from the name is the same as the permissions, that makes it easier to link the name to the permissions in the controller. With the current controller code, the $key must be the same. The Array index starts from count because there can be existing users in the view to start with
permission['index'] can not hold more than 1 value inside. You should have fullname without index and permissions like: permissions['+index+'][]. And index shoud start from 0.
@VüsalHüseynli indeed, edited, my bad. 'index should start from 0' ? You could remove the +1, but still it is not needed when using a foreach. The count is needed because of existing users in the db.
@VüsalHüseynli i removed the +1 so it initially starts from 0. I can agree coding standards say to start array indexes from 0, but it does work fine when starting from 1
$key param in foreach starts from 0. Thats why it is better to start from 0. Good job anyway.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.