为什么没有保存多个带有控制器路由返回的图像?

1l5u6lss  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(329)

在我的laravel应用程序中,我有一个保存多个图像的表单,以保存上传表,我的表单如下,

<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>

控制器的存储功能是,

$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->vehicle_id = $vehicle->id;
            $upload->save();
            return redirect()->route('categories.categorypost')->with('info','Your Advertisment has been created successfully');
        }
    }

但是当我在上面的表格中附上4张图片时。它只保存一个图像。附加到第一个输入文件的。为什么其他图像没有保存到表中。但当我删除返回代码时

return redirect()->route('categories.categorypost')->with('info','Your Advertisment has been created successfully');

然后我可以保存所有图像。但我需要以上控制器返回并保存所有图像。我该怎么做?

falq053o

falq053o1#

您的重定向位于for循环内,该循环迭代图像并保存它们,您必须将重定向放在for循环之后,否则将在保存第一个图像后重定向:

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->vehicle_id = $vehicle->id;
    $upload->save();
}
return redirect()->route('categories.categorypost')->with('info','Your Advertisment has been created successfully');

相关问题