Я использую 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";
}
}
Когда я пытаюсь загрузить и обновить, он переходит в эхо "тестирование". Не обнаружено загруженных файлов ..
Я использовал тот же код для add.blade, и он работает.
Это связано с маршрутом или иначе?
Это происходит, когда в вашей HTML-форме нет enctype = "multipart/form-data"
.
В этом случае причина в том, что 'files' => true
является частью неправильного массива внутри Form::model()
; это внутри - массив 'action'
, тогда как он должен быть вне. Попробуй это:
Form::model($dataItemregistration, [
'method' => 'PATCH',
'action' => ['Modul\ProfilController@update', $dataItemregistration->ItemRegistrationID],
'files' => true,
]);
Вы молодец ... Я несколько дней пытался найти проблему ... Спасибо, что уделили время и помогли мне ... Я принял ваш ответ.