Skip to content
Advertisement

How do I save multiple checkbox values in a single column separately in the database and retrieve it using laravel?

I want to store all the selected check box values in single-column separately like enter image description here

here is my code for that

<tr>
    <th scope="row">
        <input class="form-control" type="text" placeholder="Posts" readonly>
    </th>
    <td class="examplelink">
        <input type="checkbox" name="checkPermission[]" value="Add">
    </td>
    <td class="examplelink">
        <input type="checkbox" name="checkPermission[]" value="View">
    </td>
    <td class="examplelink">
        <input type="checkbox" name="checkPermission[]" value="Edit">
    </td>
    <td class="examplelink">
        <input type="checkbox" name="checkPermission[]" value="Delete">
    </td>
</tr>
<tr>
    <th scope="row">
        <input class="form-control" type="text" placeholder="Comments" readonly>
    </th>
    <td class="examplelink">
        <input type="checkbox" name="checkPermission[]" value="Add">
    </td>
    <td class="examplelink">
        <input type="checkbox" name="checkPermission[]" value="View">
    </td>
    <td class="examplelink">
        <input type="checkbox" name="checkPermission[]" value="Edit">
    </td>
    <td class="examplelink">
        <input type="checkbox" name="checkPermission[]" value="Delete">
    </td>
</tr>
<tr>
    <th scope="row">
        <input class="form-control" type="text" placeholder="User" readonly>
    </th>
    <td class="examplelink">
        <input type="checkbox" name="checkPermission[]" value="Add">
    </td>
    <td class="examplelink">
        <input type="checkbox" name="checkPermission[]" value="View">
    </td>
    <td class="examplelink">
        <input type="checkbox" name="checkPermission[]" value="Edit">
    </td>
    <td class="examplelink">
        <input type="checkbox" name="checkPermission[]" value="Delete">
    </td>
</tr>

And want to store individually with its supporting read-only text? how to do that ??? And when user checks the checkbox (selected) should be stored in the database?? how to write store function for it ???

here is my ui of form : enter image description here

Advertisement

Answer

If you want to store all checkbox values using the markup you showed, you won’t be able to know which permission belongs to which model.

If you have to use that exact name and can’t provide more then one key => value as post data (for some reason), you would have to add some more info to the value field.

Example:

  <tr>
        <th scope="row">
            <input class="form-control" type="text" placeholder="Comments" readonly>
        </th>
        <td class="examplelink">
            <input type="checkbox" name="checkPermission[]" value="comment_Add">
        </td>
        <td class="examplelink">
            <input type="checkbox" name="checkPermission[]" value="comment_View">
        </td>
        <td class="examplelink">
            <input type="checkbox" name="checkPermission[]" value="comment_Edit">
        </td>
        <td class="examplelink">
            <input type="checkbox" name="checkPermission[]" value="comment_Delete">
        </td>
  </tr>

Then when retrieving the data in back-end you would have to explode each value using delimiter “_” to find model at index 0 and permission at index 1.

EDIT: Or you could build a JS object containing all the data and send it as JSON data.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement