Flex's avatar
Level 4

Why did not save multiple images in to the table in Laravel 5.6

In my Laravel app I have form to save multiple images to save uploads table, my form as following,

<form method="post" action="{{url('form')}}" enctype="multipart/form-data"> 
            {{csrf_field()}}
<div class="form-group row required">
            <div class="field" align="left" >
            <h3>Upload  images</h3>
            <input type="file" class="files" name="files[]" multiple />
            <input type="file" class="files" name="files[]" multiple />
            <input type="file" class="files" name="files[]" multiple />
            <input type="file" class="files" name="files[]" multiple />
            </div>
</div>

and jquery for image,

<script>
     $(document).ready(function() {
  if (window.File && window.FileList && window.FileReader) {
    $(".files").on("change", function(e) {
      var clickedButton = this;
      var files = e.target.files,
        filesLength = files.length;
      for (var i = 0; i < filesLength; i++) {
        var f = files[i]
        var fileReader = new FileReader();
        fileReader.onload = (function(e) {
          var file = e.target;
          $("<span class=\"pip\">" +
            "<img class=\"imageThumb\" src=\"" + e.target.result + "\" title=\"" + file.name + "\"/>" +
            "<br/><span class=\"remove\">Remove image</span>" +
            "</span>").insertAfter(clickedButton);
          $(".remove").click(function(){
            $(this).parent(".pip").remove();
          });
          });
        fileReader.readAsDataURL(f);
      }
    });
  } else {
    alert("Your browser doesn't support to File API")
  }
});
 </script>

and controller store function is,

$photos = $request->file('files');

        if (!is_array($photos)) {
            $photos = [$photos];
        }

        if (!is_dir($this->photos_path)) {
            mkdir($this->photos_path, 0777);
        }

        for ($i = 0; $i < count($photos); $i++) {
            $photo = $photos[$i];
            $name = sha1(date('YmdHis') . str_random(30));
            $save_name = $name . '.' . $photo->getClientOriginalExtension();
            $resize_name = $name . str_random(2) . '.' . $photo->getClientOriginalExtension();

            Image::make($photo)
                ->resize(250, null, function ($constraints) {
                    $constraints->aspectRatio();
                })
                ->save($this->photos_path . '/' . $resize_name);

            $photo->move($this->photos_path, $save_name);

            $upload = new Upload();
            $upload->filename = $save_name;
            $upload->resized_name = $resize_name;
            $upload->original_name = basename($photo->getClientOriginalName());
            
            $upload->save();

but when I attach 4 images in above form. it is saving only one image. that attach to first input file. why other images are not saving to table. how can I fix this problem?

0 likes
3 replies
Sedi's avatar

@FLEX - Hi! Did you solve this problem? I have the same problem. I will be grateful if share the solution. Thanks!

K5AD's avatar

You can only use one input field

cause you are allowing multiple value and you are using an array

Please or to participate in this conversation.