validate([ 'products' => 'required|array', 'products.*' => 'exists:products,uuid' ]); DB::transaction(function () use ($validated, $user) { $products = array_map(fn ($v) => Product::query()->where("uuid", $v)->first(), $validated["products"]); $order = new Order($products); $order->user()->associate($user); $order->cost = array_reduce($products,fn ($c, $i) => $c+=$i->price,0); return $order->save(); }); } /** * 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"); // } }