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

jou*_*oun 5 file-upload image-uploading laravel laravel-5

我正在使用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() !!}
Run Code Online (Sandbox Code Playgroud)

控制器:

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";
    }
}
Run Code Online (Sandbox Code Playgroud)

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

我一直在为add.blade使用相同的代码,并且可以正常工作。

它与路径有关吗?

Tra*_*itz 4

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

在这种情况下,原因是'files' => true内部数组的一部分错误Form::model();当它应该在数组外部它却在数组内部。尝试这个:'action'

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