Rushikesh Ganesh
Rushikesh Ganesh

Reputation: 290

how to update multiple images in laravel?

i want store the images for product the product having multiple images the problem is that i want to update the images problem is images are updated but the entry never goes to database. instead it stores same entry two times.

if($request->hasfile('image')) {           //here i got images
    $file = $request->file('image');
    $file_count= count($file);              //for updating multiple images 

    for ($i=0; $i < $file_count; $i++) { 

        $imagesize  = $file[$i]->getClientSize();
        $imageexten = $file[$i]->getClientOriginalExtension();
        $product_image_count = count($request->productimagename);

        for ($i=0; $i < $product_image_count; $i++) { 
            if($file_count != 1) { 
                $new_name = $request->productimagename[$i].$i.".".$imageexten;
            } else {
                $new_name = $request->productimagename[$i].".".$imageexten;
            }
            $product_image_path ='images/frontendimage/product_image/'.$new_name;
            Image::make($file[$i])->save($product_image_path);    

            foreach ($product->images as $key => $value) {       
                product_image::find($value->id)->where('product_id','=',$product->id)
                ->update(['image_name'=> $new_name,'image_url'=>$product_image_path,
                    'modified_by'=>Auth::user()->id]);
            } 
        }
    } 
}

Upvotes: 0

Views: 5433

Answers (2)

Aswin Mohan
Aswin Mohan

Reputation: 31

if ($request->has('unit_images'))
        {
            $promotion = [];
            foreach ($request->file('unit_images') as $key => $file) {


                $filenameWithExt = $file->getClientOriginalName();

                $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);

                $extension = $file->getClientOriginalExtension();

                $fileNameToStore = uniqid() . '.' . $extension;

                $path = $file->storeAs("$directory/unit_images/$b/$unit_num", $fileNameToStore, 'public');

                $this->validate($request, [
                    'images' => 'dimensions:min_width=250,min_height=250'
                ]);

                $b_id = $request->building_name;

               $unitimg = UnitImage::where('unit_id', '=', $unit_store_id)->get();

               $new=strlen($unitimg);

                if ($new!='2') {

                    foreach ($unitimg as $get2) {

                        $get2->delete();
                    }

                    $nn=New UnitImage();
                    $nn->images = $path;
                    $nn->unit_id = $unit_store_id;
                    $nn->save();
                }

                if ($new=='2') {

                         $nn = New UnitImage();
                         $nn->images = $path;
                         $nn->unit_id = $unit_store_id;
                         $nn->save();
                }

            }

        }

Upvotes: 1

Raju Rayhan
Raju Rayhan

Reputation: 101

Input Field -

<input type="file" name="images[]"  class="form-control"  multiple required />

Controller -

$imagesName = array();
$imagesPath = array();

if($files=$request->file('images')){
    foreach (Input::file('images') as $file) {
        $name           = strtolower($file->getClientOriginalName());
        $name           = preg_replace('/\s+/', '_', $name);
        $img            = Image::make($file);

        $path           = 'your/path/'.$name;

        $img->save($path);

        $imagesName[]   = $name;
        $imagesPath[]   = $path;
    }
}

This will upload multiple images to your server and you will get uploaded image names from $imagesName[] and path from $imagesPath[].

Now you can use those according to your BD structure.

Upvotes: 0

Related Questions