为什么没有在Laravel5.6中更新编辑视图文件中的图像?

7tofc5zh  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(400)

我在我的laravel应用程序中编辑了blade文件。在此编辑表单中,更新保存到两个表中的数据到车辆并上载。我有以下代码在编辑刀片文件更新图片上传表,

@foreach( $vehicles-> uploads as $upload)

                    <img id="preview"
                         src="{{asset((isset($upload) && $upload->resized_name!='')?'images/'.$upload->resized_name:'images/noimage.png')}}"
                         height="200px" width="200px"/>
                    <input class="form-control" style="display:none" name="files[]" type="file" id="files" name="_token" value="{{ csrf_token() }}" enctype="multipart/form-data">
                    <br/>
                    <a href="javascript:changeProfile();">Add Image</a> |
                  <a class="button is-outlined" href="/myads/{{$upload->id}}/delete" onclick="return confirm('Are you sure to want to delete this record?')" >Delete</a></td>
<hr>
                   @endforeach
                    @endif

更新控制器是,

$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 = Upload::find($id);
            $upload->filename = $save_name;
            $upload->resized_name = $resize_name;
            $upload->original_name = basename($photo->getClientOriginalName());
            $upload->vehicle_id = $vehicle->id;
            $upload->save();
        }

路线是,

Route::post('myads/{id}', [
    'uses' => '\App\Http\Controllers\VehicleController@update',
])->name('vehicles.edit');

但当我附加图像并单击更新按钮时,它不是上传表中的更新图像。如何解决这个问题?

btxsgosb

btxsgosb1#

你的html代码是错误的。
不要使用 style="display:none"input 元素。如果它们被隐藏,浏览器将忽略它。
不要将csrf令牌与输入文件合并。
试试这个:

@foreach( $vehicles-> uploads as $upload)
  <img id="preview"  src="{{asset((isset($upload) && $upload->resized_name!='')?'images/'.$upload->resized_name:'images/noimage.png')}}" height="200px" width="200px"/>
  <input class="form-control" name="files[]" type="file" id="files" enctype="multipart/form-data">
  {{ csrf_field() }}
  <br/>
  <a href="javascript:changeProfile();">Add Image</a> |
  <a class="button is-outlined" href="/myads/{{$upload->id}}/delete" onclick="return confirm('Are you sure to want to delete this record?')" >Delete</a>
  <hr>
@endforeach
puruo6ea

puruo6ea2#

我认为您在更新控制器中使用了错误的方法。

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

请尝试此方法将字符串转换为数组。

if (!is_array($photos)) {
    $photos = explode(' ', $photos);
}

相关问题