Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shuvoo's avatar

Multipule Image upload in laravel 6

i want to try multipule image ,, with multipule row in database . but always database updated with last one image file name ..

public function upload(Request $request)
    {
        foreach ($request->photos as $photo) {

            $image = new Image();
            $image->data_id= $request->id;
            $image->filename = $photo->store('image');
            if ($image->save()) {
                flash(__('iamgehas been updated successfully'))->success();
                return redirect()->route('home.index');
            } else {
                flash(__('Something went wrong'))->error();
                return back();
            }
        }
    }
 <form action="{{route('home.upload')}}" method="POST" enctype="multipart/form-data">
@csrf                                     
<div class="custom-file">
<input type="hidden" name="id" value="{{$r->id}}">
<input type="file" class="custom-file-input" name="photos[]" multiple>
<label class="custom-file-label">Choose file</label>
<input type="submit" name="submit" class="btn-sm btn-pill btn-success"
 value="Upload">
  </div>
</form>
0 likes
1 reply
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You return in the first iteration of the loop. You need to move your returns outside the loop (foreach)

Please or to participate in this conversation.