vendredi 30 juillet 2021

Nothing saved when uploading multiple image

i have error array to string when i uploading multiple image. this is my blade code

  <form role="form" action="" method="POST" enctype="multipart/form-data"> 
     <input type="file" class="form-control"  placeholder="Image Gallery" name="image_gallery[]" 
     required="required" multiple>
   </form>

i already put multiple on input tag

and this is the controller code. the commented code "image Gallery" is the code for uploading image gallery

public function store(Request $request)
    {   
        $this->validate($request,[
            'photo'=>'required|image|mimes:png,jpg,jpeg|max:1000',
            'slug'=>'required',
            'name'=>'required',
            'description'=>'required',
            'stock'=>'required|numeric',
            'price'=>'required|numeric',
            'sale_price'=>'required|numeric',
            'brand_id' =>'integer',
            'manufacture_id' =>'integer',
            'genre_id' => 'integer',
            'gudang_id' => 'integer',
            'weight' => 'integer',
            'status' => 'required',
            'date_arrival' => 'required_if:status,==,"preorder',
            'is_protector' => 'nullable',
            'type' => 'nullable',
            'protector_id' =>'required_if:is_protector,==,"1"',
            'image_gallery' => 'required|image|mimes:png,jpg,jpeg|max:1000',
        ]);
        $formInput= $request->except(['_token']);
   
        $file = $request->file('photo');
        $filename = $file->getClientOriginalName();
        $request->file('photo')->move('static/dist/img/',$filename);
        $formInput['photo'] = 'static/dist/img/'.$filename;

        //image Gallery
        if($request->hasFile('image_gallery')){
            foreach ($request->file('image_gallery') as $file_gallery) {
            $name =  $file_gallery->getClientOriginalName();
            $file_gallery->move('static/dist/img/',$name);
            $formInput['image_gallery'] = 'static/dist/img/'.$name;
            }
         }
        //image Gallery

        if($request->status == "preorder") {
            $formInput['date_arrival'] = $request->date_arrival;
        }

        $formInput['user_id'] = Auth::user()->id;
        $new_product = Product::create($formInput);
        if($new_product) {
            Alert::success('', 'Product Berhasil di Tambahkan');
            return redirect('admin/product');
        }
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire