I want to add two images. I can upload one image but if I try to add two it shows an error. I want to add two images. I can upload one image but if I try to add two it shows an error. I want to add two images. I can upload one image but if I try to add two it shows an error. I want to add two images. I can upload one image but if I try to add two it shows an error. I want to add two images. I can upload one image but if I try to add two it shows an error.
<?php namespace AppHttpControllers; use AppModelsProduct; use IlluminateHttpRequest; use IlluminateSupportFacadesDB; use AppHttpRequestsAdminStoreTagsRequest; class ProductController extends Controller { /** * Display a listing of the resource. * * @return IlluminateHttpResponse */ public function index() { //$tags = Product::all(); //return view('products.index', compact('tags')); $products = Product::latest()->paginate(5); return view('products.index',compact('products')) ->with('i', (request()->input('page', 1) - 1) * 5); } /** * Show the form for creating a new resource. * * @return IlluminateHttpResponse */ public function create() { return view('products.create'); } /** * Store a newly created resource in storage. * * @param IlluminateHttpRequest $request * @return IlluminateHttpResponse */ public function store(Request $request) { //$tag = Product::create($request->all()); //return redirect()->route('admin.tags.index'); $request->validate([ 'name' => 'required', 'detail' => 'required', 'color' => 'required', 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', 'imagetwo' => 'required|imagetwo|mimes:jpeg,png,jpg,gif,svg|max:512', ]); $input = $request->all(); if ($image = $request->file('image')) { $destinationPath = 'image/'; $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension(); $image->move($destinationPath, $profileImage); $input['image'] = "$profileImage"; } if ($imagetwo = $request->file('imagetwo')) { $destinationPath = 'image/'; $profileImagetwo = date('YmdHis') . "." . $imagetwo->getClientOriginalExtension(); $imagetwo->move($destinationPath, $profileImagetwo); $input['image'] = "$profileImagetwo"; } Product::create($input); return redirect()->route('products.index') ->with('success','Product created successfully.'); } /** * Display the specified resource. * * @param AppProduct $product * @return IlluminateHttpResponse */ public function show(Product $product) { return view('products.show',compact('product')); } /** * Show the form for editing the specified resource. * * @param AppProduct $product * @return IlluminateHttpResponse */ public function edit(Product $product) { return view('products.edit',compact('product')); } /** * Update the specified resource in storage. * * @param IlluminateHttpRequest $request * @param AppProduct $product * @return IlluminateHttpResponse */ public function update(Request $request, Product $product) { $request->validate([ 'name' => 'required', 'detail' => 'required', 'color' => 'required' ]); $input = $request->all(); if ($image = $request->file('image')) { $destinationPath = 'image/'; $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension(); $image->move($destinationPath, $profileImage); $input['image'] = "$profileImage"; }else{ unset($input['image']); } if ($imagetwo = $request->file('imagetwo')) { $destinationPath = 'imagetwo/'; $profileTmagetwo = date('YmdHis') . "." . $imagetwo->getClientOriginalExtension(); $image->move($destinationPath, $profileTmagetwo); $input['imagetwo'] = "$profileTmagetwo"; }else{ unset($input['imagetwo']); } $product->update($input); return redirect()->route('products.index') ->with('success','Product updated successfully'); } /** * Remove the specified resource from storage. * * @param AppProduct $product * @return IlluminateHttpResponse */ public function destroy(Product $product) { $product->delete(); return redirect()->route('products.index') ->with('success','Product deleted successfully'); } function indextwo(){ //return DB::select("select * from products"); //DB::table('products')->orderBy('id','desc')->first(); return Product::orderBy('id', 'DESC')->first(); } }
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You should remove imgTwo from your validation because it does not exist, why would you put it here? tell me the reason so I can provide solution
'imagetwo' => 'required|mimes:jpeg,png,jpg,gif,svg|max:512',
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0