0

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

2 Answers 2

1

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 ');
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

oh my what a beginner mistake, thank you for pointing it out ! +1 , btw is there any way to make my controller run more efficient and look clean ? , just asking .
0

You can do the following for multiple images:

   $images = $request->file('images');

   foreach ($images as $key => $image) {
     if ($request->hasFile('images') && $request->file('images')[$key]->isValid()) {
        $path = $request->images[$key]->store('public/images');
        $path = basename($path);

        $image = new ProductImages();
        $image->product_id = $request->get('product_id');
        $image->photo = $path;
        $image->save();
      }
   }

I hope it would helpful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.