为什么更新图片检测不到上传的文件?

Why update picture doesn't detect file uploaded?

我正在使用 laravel 5 为个人资料创建编辑表单并可以更新表单中的图片。

我想以编辑形式存储新图像。我在 edit.blade 中使用此代码由用户获取图像。

查看:

{!! Form::model($dataItemregistration,['method' => 'PATCH', 'action' => ['Modul\ProfilController@update', $dataItemregistration->ItemRegistrationID, 'files' => true] ]) !!}
<div class="form-group">
    <div class="row">
        <div class="col-lg-3"> 
            {{ Form::label('pic', 'Gambar (Saiz gambar, 250x300px)') }}
        </div>
        <div class="col-lg-7">
            {!! Form::file('gambar', array('class' => 'form-control')) !!}
        </div>
    </div>
</div>
<br>
<div class="col-lg-10 text-center">
   {!! link_to(URL::previous(),'Back', ['class' => 'btn btn-warning btn-md']) !!}
   {{ Form::submit('Update', ['class' => 'btn btn-primary']) }}
</div>
{!! Form::close() !!}

控制器:

public function update(Request $request, $id)
{
    $valueitemregistrations = Itemregistration::find($id);

    $this->validate($request,['gambar' => 'max:100000',]);
    if ($request->hasFile('gambar')) {
        // Get the file from the request
        $file = $request->file('gambar');
        // Get the contents of the file
        $content = $file->openFile()->fread($file->getSize());

        $valueitemregistrations->Picture = $content;
        $valueitemregistrations->update();

        if($valueitemregistrations) {
            return redirect('profil');
        } else {
            return redirect()->back()->withInput();
        }
    } else { 
        echo "testing";
    }
}

当我尝试上传和更新时,它会回显 "testing"。它没有检测到任何上传的文件..

我一直在为 add.blade 使用相同的代码并且它有效。

是跟路由路径有关还是其他?

当您的 HTML 表单没有 enctype="multipart/form-data".

时会发生这种情况

在这种情况下,原因是 'files' => trueForm::model() 中错误数组的一部分;它位于 内部 'action' 数组,而它应该位于 外部 。试试这个:

Form::model($dataItemregistration, [
    'method' => 'PATCH',
    'action' => ['Modul\ProfilController@update', $dataItemregistration->ItemRegistrationID],
    'files' => true,
]);