简体   繁体   English

如何更新 laravel 中的多个图像?

[英]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]);
            } 
        }
    } 
}
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();
                }

            }

        }

Input Field -输入字段 -

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

Controller - 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[] .这会将多张图片上传到您的服务器,您将从$imagesName[]获取上传的图片名称,并从$imagesPath[] imagesPath[] 获取路径。

Now you can use those according to your BD structure.现在您可以根据您的 BD 结构使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM