Laravel 5.2在上传多个图像文件时仅返回1的数组计数

I'm having trouble getting the correct number of images from the array in Laravel 5.2, and the controller is only copying over one of the images to the public/img folder and not all images I've chosen. Here is my code below:

// js code
$(".start").on("click", function(e) {
    var formData    = new FormData(),
        sumMsg      = $( "#upload .summary-message" );

    $( files.files ).each(function( inx ) {
      formData.append('files', files.files[inx]);
      alert(files.files[inx].name);
    });

    // formData.append('files', fileUpload.files[0]);

    $.ajax({
        url: "{{ url('/files/upload') }}",
        type: "POST",
        data: formData,
        enctype: 'multipart/form-data',
        contentType: false,
        processData: false
    })
    .done(function(data) {
        // log data to the console so we can see
        console.log(data);

        // here we will handle errors and validation messages
        if ( data.success != true ) {
            sumMsg.removeClass( "success" );
            sumMsg.addClass( "error" );
            sumMsg.html( data.message );
        } else {
            sumMsg.removeClass( "error" );
            sumMsg.addClass( "success" );
            sumMsg.html( data.message );
        }
    })
    // using the fail promise callback
    .fail(function(data) {
        // show any errors
        // best to remove for production
        console.log(data);

        sumMsg.removeClass( "success" );
        sumMsg.addClass( "validation-summary error" );
        sumMsg.html( "Server Error: " + data.statusText + " processing your request, please contact Ticket 2 Africa or report a bug." );
    });
});

// form.blade.php
@if (count($errors) > 0)
    <div class="validation-summary">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
<br />

<div class="control-group">
    <div class="controls">
        <span class="fileinput-button">
            <label for="files">Upload image(s) : </label>
            <input type="file" id="files" name="files[]" multiple>
        </span>
        <p class="errors">{!!$errors->first('image')!!}</p>
        @if(Session::has('error'))
            <p class="errors">{!! Session::get('error') !!}</p>
        @endif
    </div>
</div>
<div id="success"> </div>
<button type="submit" class="start">Start upload</button>
<button type="reset" class="cancel">Cancel upload</button>
<button type="button" class="delete">Delete</button>
<input type="checkbox" class="toggle">

// FileUploadController Controller
public function upload(Request $request) {
    // getting all of the post data
    // $files = array('image' => Input::file('files'));

    $files = array("image" => Input::file("files"));
    // setting up rules
    $rules = array('image' => 'required|required|image|mimes:jpeg,png,jpg,gif,svg|max:1024',);      //mimes:jpeg,png and for max size max:1024
    // doing the validation, passing post data, rules and the messages
    $validator = Validator::make($files, $rules);
    if ($validator->fails()) {
        // send back to the page with the input data and errors
        return Redirect::to('upload')->withInput()->withErrors($validator);
    } else {
        // checking file is valid.
        if (Input::file('files')->isValid()) {
            $destinationPath = public_path('img');                  // upload path
            $extension = Input::file('files')->getClientOriginalExtension();                            // getting image extension

            foreach ($files as $file) {
                $fileName = rand(11111, 99999).'.'.$extension;                                            // renameing image
                $file->move($destinationPath, $fileName);                                                 // uploading file to given path
            }

            // sending back with message
            Session::flash('success', 'Upload successfully'); 
            return Redirect::to('auth/packagesandspecials');
        } else {
            // sending back with error message.
            Session::flash('error', 'uploaded file is not valid');
            return Redirect::to('auth/packagesandspecials');
        }
    }
}

You are doing wrong with your js code:

$(".start").on("click", function(e) {
var formData    = new FormData(),
    sumMsg      = $( "#upload .summary-message" );

$( files.files ).each(function( inx ) {
  formData.append('files', files.files[inx]);//THIS IS THE MISTAKE
  formData.append('files[]',files.files[inx]);/*THIS SHOULD BE DONE LIKE THIS BECAUSE IN YOUR CODE ON EVERY LOOP KEY 'files' data is replaced it is not appending as an array */
  alert(files.files[inx].name);
});

I managed to get the upload of images to work, but I'm sure it's not the ideal solution. Here is my code below:

// js code
$(".start").on("click", function(e) {
    var formData    = new FormData(),
        sumMsg      = $( "#upload .summary-message" );

    // TODO: This needs work, should use formData.append('files[]', files.files[inx]);
    $( files.files ).each(function( inx ) {
        formData.append('files', files.files[inx]);

        $.ajax({
            url: "{{ url('/files/upload') }}",
            type: "POST",
            data: formData,
            enctype: 'multipart/form-data',
            contentType: false,
            processData: false
        })
        .done(function(data) {
            // log data to the console so we can see
            console.log(data);

            // here we will handle errors and validation messages
            if ( data.success != true ) {
                sumMsg.removeClass( "success" );
                sumMsg.addClass( "error" );
                sumMsg.html( data.message );
            } else {
                sumMsg.removeClass( "error" );
                sumMsg.addClass( "success" );
                sumMsg.html( data.message );
            }
        })
        // using the fail promise callback
        .fail(function(data) {
            // show any errors
            // best to remove for production
            console.log(data);

            sumMsg.removeClass( "success" );
            sumMsg.addClass( "validation-summary error" );
            sumMsg.html( "Server Error: " + data.statusText + " processing your request, please contact Ticket 2 Africa or report a bug." );
        });
    });
});

// form.blade.php
@if (count($errors) > 0)
    <div class="validation-summary">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
<br />

<div class="control-group">
    <div class="controls">
        <span class="fileinput-button">
            <label for="files">Upload image(s) : </label>
            <input type="file" id="files" name="files[]" multiple>
        </span>
        <p class="errors">{!!$errors->first('image')!!}</p>
        @if(Session::has('error'))
            <p class="errors">{!! Session::get('error') !!}</p>
        @endif
    </div>
</div>
<div id="success"> </div>
<button type="submit" class="start">Start upload</button>
<button type="reset" class="cancel">Cancel upload</button>
<button type="button" class="delete">Delete</button>
<input type="checkbox" class="toggle">

// FileUploadController controller
public function upload(Request $request) {
    $files = array("image" => Input::file("files"));
    // setting up rules
    $rules = array('image' => 'required|required|image|mimes:jpeg,png,jpg,gif,svg|max:1024',);        //mimes:jpeg,png and for max size max:1024
    // doing the validation, passing post data, rules and the messages
    $validator = Validator::make($files, $rules);
    if ($validator->fails()) {
        // send back to the page with the input data and errors
        return Redirect::to('upload')->withInput()->withErrors($validator);
    } else {
        // checking file is valid.
        if (Input::file('files')->isValid()) {
            $destinationPath = public_path('img/packages and specials/holiday types');              // upload path
            $extension = Input::file('files')->getClientOriginalExtension();                        // getting image extension

            foreach ($files as $file) {
                $fileName = rand(11111, 99999).'.'.$extension;                                      // renameing image
                $file->move($destinationPath, $fileName);                                           // uploading file to given path
            }

            // sending back with message
            Session::flash('success', 'Upload successfully'); 
            return Redirect::to('auth/packagesandspecials');
        } else {
            // sending back with error message.
            Session::flash('error', 'uploaded file is not valid');
            return Redirect::to('auth/packagesandspecials');
        }
    }
}