My problem is that I want to update the quantity_served
based on quantity and insert the last loop value into the last record based on the input value.
For example, my input value has 180.
My code doesn't insert the last value of loop instead of the full value of quantity.
$var = $request->get('id');
$data = bulkcorporatemodel::orderBy('id', 'asc');
foreach ($data as $item)
{
if ($var == 0 || $var < 0)
{
break;
}
$bulkcorp = bulkcorporatemodel::find($item->id);
$dummyHolder = $bulkcorp->quantity;
$bulkcorp->quantity_served = $dummyHolder;
$bulkcorp->save();
$success_output = '<div class="alert alert-success">BULK DATA UPDATED</div>';
$var = $var - $dummyHolder;
}
(edit: I noticed that $item is in fact already in your $data collection, there is no need to find the item, it's already there. I have adapted the code accordingly and shorten the code all together :) )
The keyword is
update()
The thing not clear to me is what the relationship between
$var
and$bulkcorp->quantity
is. If the var is indeed an id, you will get under 0 in one cycle and the rest of the $data collection won't be executed. If$request->get('id')
is a high number, no problem.But, this is what you really want to achieve. Normally, we should provide you this code, but ok, for once: