$user->orders]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create(Request $request) { $validated = $request->validate([ 'products' => 'required|array', 'products.*' => 'exists:products,uuid' ]); $products = array_map(fn ($v) => Product::query()->where("uuid", $v)->first(), $validated["products"]); return view("order/create", ["products" => $products]); // } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $user = Auth::user(); if (!$user) { throw new AccessDeniedHttpException("Not logged in"); } $validated = $request->validate([ 'products' => 'required|array', 'products.*' => 'exists:products,uuid', 'address' => 'required', ]); $products = array_map(fn ($v) => Product::query()->where("uuid", $v)->first(), $validated["products"]); DB::transaction(function () use ($validated, $user, $products) { $address = new Address($validated); $address->user()->associate($user); $address->save(); $order = new Order(); $order->address()->associate($address); $order->user()->associate($user); $order->cost = array_reduce($products, fn ($c, $i) => bcadd($c,$i->price), 0); $order->save(); foreach($products as $product) { $order->products()->attach($product); } }); foreach($products as $product) { $user->cart()->detach($product); } } /** * Display the specified resource. * * @return \Illuminate\Http\Response */ public function show(Order $order) { // } /** * Show the form for editing the specified resource. * * @return \Illuminate\Http\Response */ public function edit(Order $order) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function update(Request $request, Order $order) { $validated = $request->validate([ 'address' => 'string', ]); if (isset($validated["address"])) { $address = $order->address()->withDefault(fn () => new Addres($validated)); $address->address = $validated["address"]; $order->save(); } } /** * Remove the specified resource from storage. * * @return \Illuminate\Http\Response */ public function destroy(Order $order) { throw new Error("TODO"); // } }