form update with file upload laravel - php

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.

Related

NotFoundHttpException en laravel 5.2

i'm using laravel 5.2 and i'm trying to define routes, but i don't know why i get NotFoundHttpException when i try to do post.
<form action="POST" action="post_to_me">
<input type="text" name="name" >
<input type="submit">
</form>
this is my form, very simple.
route::post('post_to_me',function(){
echo "post";
});
here i define the route to post, but when i run tha app, NotFoundHttpException comes out. Does anyone know why?
I think you have missed adding the CSRF Token to your form. Try adding
// Vanilla PHP
<?php echo csrf_field(); ?>
or
// Blade Template Syntax
{{ csrf_field() }}
just below your form tag

Assist with using native html form in laravel

Hello: I have done tons of html forms in wordpress and am now trying to do the same in laravel. The problem is with submitting the form; it is not going to my destination or passing the post variables as I would expect. (i am getting errors on the loading of the page).
I know it has something to do with the "routes" and possibly also CSRF? (been reading a lot on this and seeing all kinds of info
I have seen things about using Laravel to build a form "open form/close form" but I am trying to find a way to just use an html form
I have the default laravel installed with nothing extra...
I tried adding a "post" route but that did not help...
here is what i have now:
this is from my routes.php file:
Route::post('gz_form', ['as' => 'gz_form', 'uses' => 'cont15_gzap#gzap_cont_function']);
here is the top of my form:
<form method="post" autocomplete="off" action="{{ route('gz_form') }}" >
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<input type="hidden" name="gc_post" value=2 />
(I threw on that token input as some people suggested that...)
Anyway - I am hoping someone can help me with this...
you have to check your route method .. is it post or get .. and check if your route named already or not ..
Route::post('/gz_form', 'YourController#handler')->name('gz_form');
while you using {{ route('gz_form') }} you need to name it
I was able to get this to work using both of your inputs - but once i had it down to the token mismatch - i finally got it to work by changing the input on my form to:
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
i found that somewhere and by doing that the token mismatch went away...
But thanks for your help guys - getting this to work took more than one step and so your help was needed and helpful...

Laravel error TokenMismatchException&

I try to register user(using angular js).This is my form:register.blade.php
<div class="panel-footer" id="free-panel-footer">
<form
name="registerCtrl.registerForm"
ng-submit="registerCtrl.submit(registerCtrl.registerForm.$valid)"
novalidate
class="register-form"
autocomplete="off">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
....
In head index file i put this:
<meta name="csrf-token" content="{!! csrf_token() !!}">
but i got error TokenMismatchException in compiled.php line 2927&?
Try replacing that hidden input you wrote manually with {!! csrf_field() !!}. That will print the token for you and you'll be sure it's the correct name and value.
Since you are not submitting the form in a default GET or POST request, you need to add that field manually to the AJAX call you're doing, and you can use that generated field to gather the name and the value of the csrf-token. You will need to do something like this for your javascript.
Just in case nothing of this works, and under your own responsability for the security hole you will create, you can exclude that concrete route from needing the csrf-token on app/Http/Middleware/VerifyCsrfToken middleware, adding the path to the $except array.

Delete handled by resource controller doesn't work - Laravel 5.3

I'm using resource controller in Laravel 5.3 and I'm having problem with deleting a record. I would like to use simple HTML code and I know that I have to add a hidden method input to make it work.
My code is very simple:
<form action="{{ url('/task', $task->id) }}">
{{ method_field('DELETE') }}
<input type="submit" value="Delete" />
</form>
After clicking submit app redirects to blank page - it doesn't go to destroy function in controller. I don't have any idea, why it's not working. I'm not using facades, is it necessary in operation like this? I'll be very glad for every tip, thank you.
You're most likely running into a TokenMismatchException. Laravel considers the DELETE method a "writable" method, so it expects a CSRF token.
You can either add a CSRF token to your form, or, if appropriate, you can add your URI to the except array in your app/Http/Middleware/VerifyCsrfToken.php file.
To add the token to your form:
<form action="{{ url('/task', $task->id) }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<input type="submit" value="Delete" />
</form>

How to use form element in Laravel 5.2 without using form facade?

As I am new to laravel framework, I have a query, I am using <form> tag in blade template so that I can delete the data from table.
I am using this the below code of form tag to delete the data
<form action="{{ route('admin.states.update',$data->state_id) }}" id="form_sample_2" class="form-horizontal" novalidate="novalidate" method="PUT">
Here I have used method as PUT, but browser is automatically considering it as GET request, I found some questions on stackoverflow where many of them said PUT & DELETE is not detected by browser.
So using Laravel Facade Form , this problem is solved
{!! Form::open(array('route'=>['admin.states.update',$data->state_id],'role'=>'form','method'=>'PUT')) !!}
The above code work as intended but my query is I don't want to use Formfacade in Laravel , I want to use first type of HTML code for form opening.
Is there any other method by which I can use PUT method in HTML Form Tag without using any Form FAcade in Laravel.
set form method to post and add a hidden input as following
<input type="hidden" name="_method" value="put">
and also make sure to add
<input type="hidden" name="_token" value="{{ csrf_token() }}">
If your ValidateCSRF middleware is enabled.

Categories