joun
joun

Reputation: 664

Why update picture doesn't detect file uploaded?

I am using laravel 5 to create an edit form for a profile and can update picture in the form.

I want to store a new image in edit form. I use this code in edit.blade to get the image by user.

View:

{!! 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() !!}

Controller:

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";
    }
}

When I try to upload and update, it goes to echo "testing". It doesn't detected any files uploaded..

I had been using the same code for add.blade and it works.

Is it related to route path or else?

Upvotes: 5

Views: 184

Answers (1)

Travis Britz
Travis Britz

Reputation: 5552

This happens when your HTML form doesn't have enctype="multipart/form-data".

In this case the cause is 'files' => true being part of the wrong array inside Form::model(); it's inside the 'action' array when it should be outside. Try this:

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

Upvotes: 4

Related Questions