Почему обновление изображения не обнаруживает загруженный файл? - PullRequest
0 голосов
/ 30 октября 2018

Я использую 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, и он работает.

Это связано с путем маршрута или еще?

1 Ответ

0 голосов
/ 07 ноября 2018

Это происходит, когда ваша HTML-форма не имеет enctype="multipart/form-data".

В этом случае причина в том, что 'files' => true является частью неверного массива внутри Form::model(); это внутри массива 'action', когда оно должно быть снаружи . Попробуйте это:

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