i want this code should not be run if user call upate route code
if($getDistanceValue * 1000 > 300){ if($chk_ord == 0) { $order->chk_ord_vst = 1; } elseif($chk_ord != 0){ $order->chk_ord_vst = $chk_ord + 1; } } elseif($getDistanceValue * 1000 <= 300 ){ if($chk_ord >= 4){ $order->chk_ord_vst = 2; } elseif($chk_ord == 3){ $order->chk_ord_vst = 2; } elseif($chk_ord == 2){ $order->chk_ord_vst = 1; } elseif($chk_ord <= 1){ $order->chk_ord_vst = 0; } }
i am using a same fucntion to update or store order but on update i donot want to run above code i know it is very simple but i am a student and learning help me here it is controller function first user create order the if wants to update then we use get function to pass value to edit view and then pass again to stor function
public function storeOrder(Request $request , $update = null , $customer_id){ $order = !is_null($update) ? Order::find($update) : new Order(); $cus_det = explode("-", $request->customer_id); $tt_amount = array_sum($request->amount) + $request->old_balance; if(is_null ($update)) $balance = $tt_amount + $request->old_balance; $auth_id = Auth::id(); if(Auth::user()->role == 4){ $auth_id = Customer::where('user_id', $auth_id)->first()->created_by; } $order->customer_id = $cus_det[0]; $order->user_id = Auth::id(); $order->ot_id = !is_null($update) ? $order->ot_id : $auth_id; $order->unit = array_sum($request->unit); $order->amount = $tt_amount; $order->subtotal = array_sum($request->amount); $order->order_comments = $request->order_comments; if( Auth::user()->role == 5){ $order->location_url_ot = $request->location_url_ot; $order->received_amount = 0; } else{ $order->received_amount = $request->received_amount; } $order->discount = $request->discount; $order->order_date= date('Y/m/d', strtotime($request->order_date)); if($tt_amount >= $request->received_amount){ $order->amount_left = ($tt_amount - $request->received_amount - $request->discount); } else{ $order->advance = $request->received_amount - $tt_amount; $order->amount_left = $request->old_balance - $request->received_amount - $request->discount; } $checkB = $this->checkMinBalance($cus_det[0] , $order->amount_left); if($checkB){ return redirect()->back()->with('error' , 'Customer Balance Limit Exceeded ( Limit is '.$checkB.' )'); } if($request->has('important')){ $order->is_important=1; } if($request->has('urgent')){ $order->urgent = urgent ; } $getothomDistanceValue = ($this->getothomDistance($order) * 1.37); $getDistanceValue = ($this->getDistance($order) * 1.37); $old_order = Order::where('customer_id' , $order->customer_id)->orderBy('id' , 'desc')->get(); $chk_ord = $old_order[0]->chk_ord_vst; if($getDistanceValue * 1000 > 300){ if($chk_ord == 0) { $order->chk_ord_vst = 1; } elseif($chk_ord != 0){ $order->chk_ord_vst = $chk_ord + 1; } } elseif($getDistanceValue * 1000 <= 300 ){ if($chk_ord >= 4){ $order->chk_ord_vst = 2; } elseif($chk_ord == 3){ $order->chk_ord_vst = 2; } elseif($chk_ord == 2){ $order->chk_ord_vst = 1; } elseif($chk_ord <= 1){ $order->chk_ord_vst = 0; } } $order->save(); }
Advertisement
Answer
What I get from your question is you are using the same storeOrder
function for storing and updating an order.
When you are using it for update, You need the id from $request->id or any other parameter from the front-end so that you can find that specific Order and then call update(). If you get the $id, You can just check if the order exists or not like below:
$order_exists = Order::find($request->id); if(!isset($order_exists)) { if($getDistanceValue * 1000 > 300){ if($chk_ord == 0) { $order->chk_ord_vst = 1; } elseif($chk_ord != 0){ $order->chk_ord_vst = $chk_ord + 1; } } elseif($getDistanceValue * 1000 <= 300 ){ if($chk_ord >= 4){ $order->chk_ord_vst = 2; } elseif($chk_ord == 3){ $order->chk_ord_vst = 2; } elseif($chk_ord == 2){ $order->chk_ord_vst = 1; } elseif($chk_ord <= 1){ $order->chk_ord_vst = 0; } } }
// then $order->save() when done