/** * Run the database seeds. * * @return void */ public function run() { OrderItem::create(['order_id' => 1, 'product_id' => 2, 'quantity' => 1, 'subtotal_price' => 15]); OrderItem::create(['order_id' => 2, 'product_id' => 1, 'quantity' => 1, 'subtotal_price' => 25]); OrderItem::create(['order_id' => 2, 'product_id' => 2, 'quantity' => 1, 'subtotal_price' => 15]); OrderItem::create(['order_id' => 3, 'product_id' => 2, 'quantity' => 2, 'subtotal_price' => 30]); OrderItem::create(['order_id' => 4, 'product_id' => 3, 'quantity' => 1, 'subtotal_price' => 1000]); OrderItem::create(['order_id' => 5, 'product_id' => 1, 'quantity' => 2, 'subtotal_price' => 50]); OrderItem::create(['order_id' => 6, 'product_id' => 2, 'quantity' => 1, 'subtotal_price' => 15]); OrderItem::create(['order_id' => 7, 'product_id' => 2, 'quantity' => 2, 'subtotal_price' => 30]); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * * @return \Illuminate\Http\Response */ public function store(Request $request) { $qtys = $request->get('qty'); $charge = Cart::where('user_id', Auth::user()->id)->get()->map(function ($c) { return $c->product; })->sum(function ($p) use($qtys) { return $p->price * $qtys[$p->id]; }); $order = Order::create(['user_id' => Auth::user()->id, 'note' => $request->get('note'), 'charge' => $charge, 'status' => 'Cooking', 'address' => $request->get('address')]); foreach ($qtys as $pid => $qty) { OrderItem::create(['product_id' => $pid, 'order_id' => $order->id, 'qty' => $qty]); } Cart::where('user_id', Auth::user()->id)->delete(); return redirect('/')->with('success', 'Your order will be delivered to ' . $request->get('address') . ' soon.'); }
public function run() { $faker = $this->getFaker(); $orders = Order::all(); $equipment = Equipment::all()->toArray(); foreach ($orders as $order) { $used = []; for ($i = 0; $i < rand(1, 5); $i++) { $product = $faker->randomElement($equipment); if (!in_array($equipment["id"], $used)) { $id = $equipment["id"]; $price = $equipment["price"]; $quantity = rand(1, 3); OrderItem::create(["order_id" => $order->id, "equipment_id" => $id, "price" => $price, "quantity" => $quantity]); $used[] = $equipment["id"]; } } } }
protected function saveOrderItem($item, $order_id) { OrderItem::create(['quantity' => $item->quantity, 'price' => $item->price, 'product_id' => $item->id, 'order_id' => $order_id]); }
public function review() { $rules = array('name' => 'required|min:5', 'email' => 'required|email', 'address1' => 'required|min:5', 'city' => 'required', 'postcode' => 'required|min:3', 'country' => 'required|max:2', 'payment_method' => 'required|max:6'); $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { return redirect()->back()->withErrors($validator->errors())->withInput(); } else { $addressinfo = Input::except('_token', 'page', 'cartjson'); $order = Order::create($addressinfo); $order->ordernumber = "LEAF-" . mt_rand(100000, 999999); $cart = json_decode(Input::get('cartjson')); $totalamount = 0; foreach ($cart->items as $cartitem) { $item = Item::find($cartitem->itemid); if ($item->id == Item::SHIPPING) { $shipping = $this->calculateShipping($order); $orderitem = OrderItem::create(["item_id" => $item->id, "order_id" => $order->id, "itemprice" => $shipping, "amount" => 1]); $totalamount += $shipping; } else { $orderitem = OrderItem::create(["item_id" => $item->id, "order_id" => $order->id, "itemprice" => $item->price, "amount" => $cartitem->itemamount]); $totalamount += $item->price * $orderitem->amount; } $orderitem->save(); } $order->totalamount = $totalamount; $order->save(); return view('review', ["order" => $order]); } }
/** * Save order item * * @param $name * @param $amount * @param $orderId */ public function save($name, $amount, $orderId) { OrderItem::create(['name' => $name, 'amount' => $amount, 'order_id' => $orderId]); }
public function putPlace(Request $request) { $validator = Validator::make($request->input(), ['addr' => 'required|string', 'phone' => 'required|numeric'], ['addr.required' => 'The address field is required.', 'phone.required' => 'The phone number is required.', 'phone.numeric' => 'The phone number must be numeric.']); if ($validator->fails()) { return redirect('order/place')->withErrors($validator)->withInput(); } $order = Order::create(['addr' => $request->input('addr'), 'phone' => $request->input('phone'), 'user_id' => $request->user()->id]); $order->save(); $cart = $request->session()->get('cart', array()); foreach ($cart as $product_id => $count) { $product = Product::find($product_id); $orderItem = OrderItem::create(['order_id' => $order->id, 'product_id' => $product->id, 'weight' => $count, 'price' => $product->price]); $orderItem->save(); } $request->session()->forget('cart'); //The cart should be empty after an order. return redirect('order/show/' . $order->id); }