我正在使用laravel 5为配置文件创建一个编辑表单,并可以更新表单中的图片。
我想在编辑表单中存储一个新图像。我在edit.blade中使用此代码由用户获取图像。
视图:
{!! Form::model($dataItemregistration,['method' => 'PATCH', 'action' => ['ModulProfilController@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";
}
}
当我尝试上传和更新时,它会进行回声"测试"。它没有检测到任何上传的文件。。
我一直在为add.blade使用相同的代码,它很有效。
是否与路径有关?
当HTML表单没有enctype="multipart/form-data"
时,就会发生这种情况。
在这种情况下,原因是'files' => true
是Form::model()
内错误阵列的一部分;当它应该在外部时,它在CCD_ 4阵列内部。试试这个:
Form::model($dataItemregistration, [
'method' => 'PATCH',
'action' => ['ModulProfilController@update', $dataItemregistration->ItemRegistrationID],
'files' => true,
]);