I have the store method and it works well when creating one, the table update increases quantity correctly. but when I delete that Item which I created the table doesn't update at all, and it must decrease the quantity
$products = $request->items;
for ($i = 0; $i < count($products); $i++) {
$billProduct = new BillProduct();
$billProduct->bill_id = $bill->id;
$billProduct->product_id = $products[$i]['item'];
$billProduct->quantity = $products[$i]['quantity'];
$billProduct->save();
Utility::total_quantity('plus',$billProduct->quantity,$billProduct->product_id);
and this is destroy the controller
if ($bill->created_by == \Auth::user()->creatorId()) {
$bill->delete();
if ($bill->vender_id != 0) {
Utility::userBalance('vendor', $bill->vender_id, $bill->getTotal(), 'debit');
}
BillProduct::where('bill_id', '=', $bill->id)->delete();
and this is a model in which - or + quantity
Utility::total_quantity('plus',$billProduct->quantity,$billProduct->product_id);
Utility::total_quantity('munis',$billProduct->quantity,$billProduct->product_id);
how can I fix this problem in destroy?? when I put the minus code in destroy methods get server error 500.
When you use $bill->delete();
, $bill->vender_id
will set to null. Because of no record found. What you can do is as I understood,
if ($bill->created_by == \Auth::user()->creatorId()) {
$billProducts = BillProduct::where('bill_id', '=', $bill->id)->get();
# If multiple data, remove from the quantity
foreach ($billProducts as $billProduct) {
Utility::total_quantity('minus', $billProduct->quantity, $billProduct->product_id);
}
# First delete product then bill
BillProduct::where('bill_id', '=', $bill->id)->delete();
$bill->delete();
}