Search code examples
phplaravelimage-uploadintervention

Multiple resize image upload with Laravel


I'm working to make a multi upload image to database with intervention resizer in Laravel.

This is what I'm coding right now in my controller imgProdukProc controller:

use Illuminate\Http\Request;
use File;
use Image;
use App\imgProd;

.....

public function store(Request $request)
    {
        if($request->hasFile('img')){
            foreach ($request->file('img') as $image) {
            if ($image->isValid()) {

                $img = new imgProd();
                $image_name = uniqid() .'.'. $image->getClientOriginalExtension();
                $path = public_path('/img/prod');
                $imgx = Image::make($image->getRealPath());
                $imgx->resize(360, 360, function ($constraint) {
                $constraint->aspectRatio();
                })->save($path.'/'.$image_name);

                $img->id_prod = $request->get('id_prod');
                $img->pics = 'img/prod/'.$image_name;

                $date=date_create('now');
                $format = date_format($date,"Y-m-d");
                $img->date = $format;
                $img->save();
                return redirect('adminImgProd')->with('success', 'Picture successfully added ');
            }
        }          
    }
}

and this is my views

adminImgProd Views

<form enctype="multipart/form-data" action="{{url('adminImgProd')}}" method='post'>
@csrf

<div class="form-group">
<label>CODE PRODUCT</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-pencil"></span></span>
<input type="text" autocomplete="off" class="form-control" id='id_prod' name='id_prod' required="required" />
<span class="input-group-addon"><button type="button" onClick="javascript:openWindow2();">Select</button></span>
</div>
</div>

<div class="form-group">
<label>IMAGE PRODUCT</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-pencil"></span></span>
<input multiple type="file" class="form-control" name='img[]' id='file-input' required="required" /></div>
</div>

Code above is working , but somehow when I tried to upload 2 images or 3 images the only image saved both in target folder and in database is only one and it is the last one

Where is my code mistake , or just my code simply wrong from the start ?

Thank you in advance


Solution

  • You put your return in your foreach, so after 1 loop, it will return and exit your function :

    public function store(Request $request)
    {
        if ($request->hasFile('img')){
            foreach ($request->file('img') as $image) {
                if ($image->isValid()) {
    
                    $img = new imgProd();
                    $image_name = uniqid() .'.'. $image->getClientOriginalExtension();
                    $path = public_path('/img/prod');
                    $imgx = Image::make($image->getRealPath());
                    $imgx->resize(360, 360, function ($constraint) {
                        $constraint->aspectRatio();
                    })->save($path.'/'.$image_name);
    
                    $img->id_prod = $request->get('id_prod');
                    $img->pics = 'img/prod/'.$image_name;
    
                    $date=date_create('now');
                    $format = date_format($date,"Y-m-d");
                    $img->date = $format;
                    $img->save();
                }
            }
    
            return redirect('adminImgProd')->with('success', 'Picture successfully added ');
        }
    }