0

Hi I have a row of data where I will like to allow the users to click on a checkbox on/off the data for use. I have read up on https://stackoverflow.com/a/43995087/14936674 answer and it is working fine, except that my checkbox value can only be update if it is checked. If I want to uncheck the box and update it, there will be no update done to it. Currently I have tested out just by updating the checkbox, however it will update and uncheck all of my data.

Below are my blade.php

<tbody>
@foreach($operatinghrs as $i => $operatinghr)
<tr id="{{ $operatinghr->{'opID'} }}">
<td>
<input type="hidden" name="operatinghrs[{{ $i }}][opID]" value="{{ $operatinghr->{'opID'} }}">{{ $operatinghr->{'day'} }}
</td>                                    
<td>
<input type="time" id="start_time" name="operatinghrs[{{ $i }}][start_time]" min="00:00" max="23:59" value="{{  display24HTime(old('start_time', $operatinghr->start_time))}}">
</td>
<td>
<input type="time" id="end_time" name="operatinghrs[{{ $i }}][end_time]"  min="00:00" max="23:59" value="{{ display24HTime(old('end_time', $operatinghr->end_time))}}">
</td>
<td>
<div class="switch"> 
<input type="checkbox" id="clinic_open" name="operatinghrs[{{ $i }}][clinic_open]" class="switch-input" value="1" {{ old('clinic_open', $operatinghr->clinic_open=="true") ? 'checked="checked"' : '' }}/>
<div class="circle"></div>
</div> 
</td>   
</tr>
@endforeach
</tbody>

Below are the codes in my controller class:

 public function update(Request $request)
    {
        foreach($request->get('operatinghrs', []) as $operatinghr) {
            $db = new OperatingHour();
            $db->where('opID', $operatinghr['opID'])->where('clinic_id', '=', $_SESSION['clinic_ID'])
                ->update(
                    [
                    'clinic_open' => $request->input('clinic_open') ? true : false, 
                    
                ]
            );
        }

        return back()->with('success_message', 'Details updated successfully!');
    }

2 Answers 2

1

If you want to only update where $operatinghr['clinic_open'] value is true condition.

Consider this from MDN:

If a checkbox is unchecked when its form is submitted, there is no value submitted to the server to represent its unchecked state (e.g. value=unchecked); the value is not submitted to the server at all. If you wanted to submit a default value for the checkbox when it is unchecked, you could include an inside the form with the same name and value, generated by JavaScript perhaps.

Also you must access clinic_open value over $operatinghr as $operatinghr['clinic_open'] for specific entry. Not as $request->input('clinic_open')

So solution must like that:

// Get all Operating values.
$operatinghrs = $request->get('operatinghrs', []);

/* 
 * Filter values with clinic_open criteria.  
 * Checkbox values are only present when is checked. 
 * So isset control work for us.
 */ 
$operatingsForUpdate = array_filter($operatinghrs, function($op){
    return isset($op['clinic_open']); // return only has clinic_open value.
});

// Loop for only for update operatings.
foreach($operatingsForUpdate  as $operatinghr) {
     // Update specific entry which contain clinic_open value is true.           
}



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

4 Comments

Hi thanks for your input! What if I want to update entries that have clinic_open value is false? As I have case where the user may want to set clinic_open value to be false when it was previously true
You mustn't filter posted $operatinghrs and loop for each item. After that, inside loop, you must get current operatinghrs from db. And check clinic_open isset condition and check specific item current(which come from db) clinic_open value. if clinic_open not exists and current item value true. make item value false.
Hi i did a if else loop within my foreach method: foreach($request->get('operatinghrs', []) as $operatinghr) { if(array_has($operatinghr, ['clinic_open'])){ $db = new OperatingHour(); $db->where('opID', $operatinghr['opID'])->where('clinic_id', $_SESSION['clinic_ID'])->update(array_except($operatinghr, ['opID'])); } else{ $db = new OperatingHour(); $db->where('opID', $operatinghr['opID'])->where('clinic_id', $_SESSION['clinic_ID']) ->update([ 'start_time' => $operatinghr['start_time'], 'end_time' => $operatinghr['end_time'], 'clinic_open' => false, ] ); } } and it is working
Looks good. I think you can get current items through one query with posted Ids outside of loop. And you can sync current items with posted values ​​for avoid performance issues.
0

Working with checkbox is always tricky, the default values of a checkbox are on and off, but if the checkbox is unchecked it won't appear in request data.

So you need to validate if the checkbox exists in request before try to update, and if not, save a string 'off' in the database or don't save anything and set a default value ('off') for that field, when you retrieve the data in the edit/update form, all the checkbox with 'off' value will be unchecked.

Using the has method on the $request, like this:

if($request->has('clinic_open')) {
    foreach($request->get('operatinghrs', []) as $operatinghr) {
      $db = new OperatingHour();
      $db->where('opID', $operatinghr['opID'])->where('clinic_id', $_SESSION['clinic_ID'])
                ->update(['clinic_open' => $request->input('clinic_open')]);
        }  
      }

Some advices:
Avoid using the '=' condition on where methods, laravel asummes it, save time and space.
Remove the value='1' of the checkbox, leave some work to the browser and do less.

1 Comment

Hi thanks for your input! I have tried out what you have suggested, however in the if($request->has('clinic_open')) in my dd() output it has return false even though I have checked some of my checkbox. Also my value for clinic_open in my db is boolean where its true or false

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.