2

I am trying to save the values from html checkboxes in a MySQL database table but I am not doing it right. I need your suggestions here.

This is my html

@foreach($sql as $sql)
<div class="form-group">
    <label class="control-label mb-10" for="">{{$sql->name}}</label>
    <div class="input-group">
        <input type="hidden" name="resource[]" value="{{$sql->id}}">
        <input type="checkbox" name="resources[]" value="c">Create
        <input type="checkbox" name="resources[]" value="r">Read
        <input type="checkbox" name="resources[]" value="u">Update
        <input type="checkbox" name="resources[]" value="d">Delete
    </div>
</div>
@endforeach

This is my controller where I am trying to save into a DB table

public function store(Request $request) {
    foreach ($request->resource as $resource) {
        # code...
        foreach ($request->resources as $resources) {
            $res[] = $resources;
            $options =  implode(',', $res); // Get selected options
            $resource = $resource; // Get value of the resource
        }
    }
}

This does not work as it only shows just one 'selected checkbox field'. Please what am I doing wrong?

2
  • 2
    For one the name of your array is resources, and you should also probably access it as $request->input('resources'). Commented Aug 7, 2017 at 18:49
  • Are you trying to save selected options separated by comma (,). for ex: c,d in database if c and d is selected ? Commented Aug 7, 2017 at 19:14

3 Answers 3

2

Looking at your HTML code, it appears you're going to be looping over possibly more than one SQL statement to make the checkboxes. The server won't be able to tell these apart. You should change your checkbox names to be more like:

<input type="checkbox" name="resources[{{$sql->id}}][]" value="c">Create
<input type="checkbox" name="resources[{{$sql->id}}][]" value="r">Read

Then your PHP code could look something like this:

foreach ($request->input('resources') as $id => $resources) {
    $options[$id] = implode(',', $resources);
}

Each SQL statement will be in the $options array keyed by the SQL id. The array value will be the checked checkboxes values separated by a comma.

print_r($options)

[
    1 => "c,r,u,d",
    2 => "c,r,d"
]
Sign up to request clarification or add additional context in comments.

Comments

0

If you are trying to save the selected values separated by comma, for ex: if user has selected by c and d, then you are saving in database as c,d ?. if so then you can get all selected values in single line.

public function store(Request $request) {
   // get all values separated by comma
   $selectedValues = implode(",", $request->input('resources'));

  // save values in database here

}

Comments

-1

remember the your checkbox is an array ! for your understating better what sent to your controller just write

print_r($_POST);exit();

to see what arriving exactly after that write it in your framework method like :

foreach($_POST['resources'] as $resources){
...
}

Comments

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.