如何在laravel中更新多张图片?

时间:2019-11-07 06:53:09

标签: php laravel image-uploading

我要存储产品的图像具有多个图像的产品问题是我要更新图像问题是图像已更新但条目从未进入数据库。而是两次存储相同的条目。

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

2 个答案:

答案 0 :(得分:1)

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

            }

        }

答案 1 :(得分:0)

输入字段-

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

控制器-

$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;
    }
}

这会将多张图片上传到您的服务器,您将从$imagesName[]获取上传的图片名称,并从$imagesPath[]获取路径。

现在,您可以根据BD结构使用它们。