/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { if ($request->get('customer_name') == null) { return response()->json(['response_order_message' => 'fail', 'response_order_message_detail' => 'Customer name is required']); } $customer = Customer::where('name', '=', $request->get('customer_name'))->first(); if ($customer == null) { $customer = new Customer(); $customer->name = $request->get('customer_name'); $customer->slug = str_slug($customer->name); $customer->phone_number = $request->get('customer_phone_number'); $customer->email = $request->get('customer_email'); $customer->save(); $customerAddress = new CustomerAddress(); $customerAddress->line1 = $request->get('line1'); $customerAddress->line2 = $request->get('line2'); $customerAddress->district = $request->get('district'); $customerAddress->province = $request->get('province'); $customerAddress->post_code = $request->get('post_code'); $customerAddress->customer_id = $customer->id; $customerAddress->save(); } $order = new Order(); $order->customer_id = $customer->id; $order->status = 0; $order->save(); $orderDetail = new OrderDetail(); $orderDetail->order_id = $order->id; $orderDetail->product_id = $request->get('product_id'); $orderDetail->amount = $request->get('amount'); $orderDetail->total_price = Product::findOrNew($orderDetail->product_id)->price * $orderDetail->amount; $orderDetail->save(); return $order->id; }
/** * @param CustomerAddress $address * @return string */ public function addAddress($address) { $uid = uniqid(); $address->setUid($uid); $_SESSION['cart_service']['addresses'][] = $address; return $uid; }