I am using the following to check if a checkbox is checked or unchecked:
In my view:
{{ Form::hidden('unchecked[]', $product->id) }} {{ Form::checkbox('checkbox[]', $product->id) }}
In my controller:
public function checkbox()
{
$checkedIDs = Input::get('checkbox');
$uncheckedIDs = Input::get('unchecked');
if(is_array($uncheckedIDs))
{
Products::whereIn('id',$uncheckedIDs)->update(['checked' => '0']);
}
if(is_array($checkedIDs))
{
Products::whereIn('id',$checkedIDs)->update(['checked' => '1']);
}
return Redirect::to('index');
}
This is working FINE! However... I also have a few hidden fields that only appear when a checkbox is clicked. A dropdown with a range of numbers that I can choose from. Once I select a checkbox, the dropdown appears and I select a number from that dropdown and I hit submit. Using the below code, everything is saved in the database:
public function checkbox()
{
$checkedIDs = Input::get('checkbox');
$uncheckedIDs = Input::get('unchecked');
$checkedNumbers = Input::get('number');
if(is_array($uncheckedIDs))
{
Products::whereIn('id',$uncheckedIDs)->update(['checked' => '0']);
}
if(is_array($checkedIDs))
{
Products::whereIn('id',$checkedIDs)->update(['checked' => '1']);
foreach($checkedNumbers as $id => $number) Products::where('id',$id)->update(['number'=>$number]);
}
return Redirect::to('index');
}
So, this actually loops through all the number[] values that have been submitted and updates the database accordingly. So far, everything works as I want.
Now for the issue. When I open my form again, the previously saved checkboxes are checked, and their corresponding dropdowns are showing the numbers as well. Once I uncheck a checkbox and save, the number does NOT get erased, while the checkbox does get unchecked once I open the form again. What I basically need, is for the following bit of code to ONLY update the numbers from the checkboxes that are checked:
foreach($checkedNumbers as $id => $number) Products::where('id',$id)->update(['number'=>$number]);
But so far I haven't been able to figure out how to accomplish this. Any pointers?
Aucun commentaire:
Enregistrer un commentaire