What I have is a grocery list. Once I select a grocery by ticking the checkbox, I can select an amount for that product and a unit (unit is referred to in my below code as group). Once I am done, I click save and the values are updated in the database. But for all checkboxes that are unchecked, the values should be updated to 0. Also, if I DO select a checkbox, but I DO NOT select an amount OR a group, the values should all still be updated to 0.
The code below works flawlessly, but I think it could be written much shorter / better. Any pointers?
public function post()
{
$checkboxes = Input::get('checkbox');
$checkedAmounts = Input::get('amount');
$checkedGroups = Input::get('group');
/* The below code checks if a checkbox was checked, an amount was selected and a group was selected. If any of those values are true, update the database with the values selected. */
foreach ($checkedAmounts as $key => $value)
{
if($value !== '0')
{
Product::where('id', '=', $key) ->update(['amount' => $value]);
}
}
foreach ($checkedGroups as $key => $value)
{
if($value !== '0')
{
Product::where('id', '=', $key) ->update(['group' => $value]);
}
}
foreach ($checkboxes as $key => $value)
{
if($value !== '0')
{
Product::where('id', '=', $key) ->update(['selected' => '1']);
}
}
/* The below code checks if a checkbox was NOT checked, an amount was NOT selected or a group was NOT selected. If ANY of those are false, update the database and set everything to 0 for that row. */
foreach ($checkboxes as $key => $value)
{
if($value === '0')
{
Product::where('id', '=', $key) ->update(['selected' => '0']);
Product::where('id', '=', $key) ->update(['amount' => '0']);
Product::where('id', '=', $key) ->update(['group' => '0']);
}
}
foreach ($checkedAmounts as $key => $value)
{
if($value === '0')
{
Product::where('id', '=', $key) ->update(['selected' => '0']);
Product::where('id', '=', $key) ->update(['amount' => '0']);
Product::where('id', '=', $key) ->update(['group' => '0']);
}
}
foreach ($checkedGroups as $key => $value)
{
if($value === '0')
{
Product::where('id', '=', $key) ->update(['selected' => '0']);
Product::where('id', '=', $key) ->update(['amount' => '0']);
Product::where('id', '=', $key) ->update(['group' => '0']);
}
}
return Redirect::to('groceries')->with('categories', Category::with('products')->orderBy('name', 'asc')->get());
}