简体   繁体   中英

form update with file upload laravel

I'm having a bit of an issue when it comes to updating a form and and having an file input. Here is what I am working with.

I have a form in laravel 5.1 which has a post method and a hidden 'Patch' method. This works as is should updating the fields that are in the form. However, when it introduce:

<input type="file" id="profile_picture" name="image_url" />

into the form, i get a:

MethodNotAllowedHttpException in RouteCollection.php line 218:

laravel error. I have tried changing the

 <input type='hidden' name='_method' value='PATCH'>

to PUT and it still doesnt like it.

My form looks like this:

<form action='{{url("profiles/$user->id")}}' method="post" class="form-horizontal" enctype="multipart/form-data">

route resource looks like this:

Route::resource('profiles', 'ProfilesController');

I can't figure out what I am missing here...Any help is much appreciated.

I believe it has to do with the exact route you are typing out in the "action" parameter matching up with the profile controller's update method.

Try changing

action'{{url("profiles/$user->id")}}'

to

action='{{ route("profiles.update", $user->id) }}'

Additionally, you could use the Laravel Collective HTML package to simply opening and closing of forms.

Also for POST Request types, you need to send the CSRF token along with your form data. If you are using laravel blade template in your view, you may use

{{ csrf_field() }}

which translates to

<input type="hidden" name="_token" value={{ csrf_token() }}

Please refer the documentation for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM