在 laravel 5.3 中尝试上传多张图片时,只有最后一张图片正在上传

Only the the last image is uploading while trying to upload multiple image in laravel 5.3

我正在尝试上传多张图片。但问题是只有最后一张图片 uploaded.Here 是我的代码

view

<input type="file" id="files" name="file[]" multiple accept="image/*">

controller

if($request->file('file')){
            $files = $request->file;
            foreach ($files as $file){
                $fileName = time().'.'.$file->getClientOriginalExtension();
                $file->move(public_path('/images'), $fileName);
                $Product = Products::select('product_id')->orderBy('product_id','desc')->first();
                $Photo = new Photo();
                $Photo->url = $fileName;
                $Product->Photo()->save($Photo);
            }

如何解决。

您的代码没有任何问题,只需 Select 一次所有文件因此您的输入字段应显示所选 images/files 个文件的数量,而不是最后一个文件名: