route("product.create"); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { $prod = new Product(); $categories = Category::all(); return view("product/edit", [ 'product' => $prod, 'categories' => $categories, 'new'=>true]); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $validated = $request->validate([ 'name'=>'required', 'description'=>'required', 'price'=>'required|numeric', 'visualization' => 'image|mimes:jpg,png,jpeg,gif,svg,webp|max:5000', ]); $product = new Product($validated); if($validated['category']) { $product->categories()->sync($validated['category']); } $product->save(); if (isset($validated["visualization"])) { $visualization = new Image(); $visualization->path = $request->file('image')->store("uploads"); $product->images()->save($visualization); } return redirect()->route("product.show", [ 'product' => $product])->with("message", "Product created successfully"); } /** * Display the specified resource. * * @param \App\Models\Product $product * @return \Illuminate\Http\Response */ public function show(Product $product) { return view("product.view", ["product" => $product]); } /** * Show the form for editing the specified resource. * * @param \App\Models\Product $product * @return \Illuminate\Http\Response */ public function edit(Product $product) { $categories = Category::all(); return view("product.edit", ["product" => $product, 'categories' => $categories, 'new'=>false]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Models\Product $product * @return \Illuminate\Http\Response */ public function update(Request $request, Product $product) { $validated = $request->validate([ 'name'=>'string', 'description'=>'string', 'price'=>'numeric', 'category' => 'array|exists:App\Models\Category,uuid', 'visualization' => 'image|mimes:jpg,png,jpeg,gif,svg,webp|max:5000', ]); $product->fill($validated); if($validated['category']) { $product->categories()->sync($validated['category']); } $product->save(); if (isset($request["image"])) { $visualization = new Image(); $visualization->path = $request->file('image')->store("uploads"); $product->images()->save($visualization); } return redirect()->route("product.show", [ 'product' => $product])->with("message", "Product created successfully"); } /** * Remove the specified resource from storage. * * @param \App\Models\Product $product * @return \Illuminate\Http\Response */ public function destroy(Product $product) { $product->delete(); return redirect()->route("product.index"); } }