Laravel controller for a grocery list
First of all, you could have less updates if you passed all values you want to set at once:

Product::where('id', '=', $key) -&gt;update(['selected' =&gt; '0', 'amount' =&gt; '0', 'group' =&gt; '0']);


Secondly, you check in many places in your code if value is different than 0 and save the value, but if later if it is equal to 0 you save 0 in the database - you could just simply save the value in the database and skip the checks.

This, including some other enhancements, should work for you:

$data = [];
foreach ($checkedAmounts as $productId =&gt; $value)  {
  $data[$productId]['amount'] = $value;
}

foreach ($checkedGroups as $productId =&gt; $value) {
  $data[$productId]['group'] = $value;
}

foreach ($checkboxes as $productId =&gt; $value) {
  $data[$productId]['selected'] = boolval($value);
}

foreach ($data as $productId =&gt; $updates) {
  Product::whereId($productId)-&gt;update($updates);
}