Laravel POST failing - MethodNotAllowedHttpException (405) - even with POST Route and CSRF defined - php

I have a form with the method post set and the csrf_field() included:
<form method="post">
{{ csrf_field() }}
...
<button type="submit" class="btn btn-xs btn-space btn-primary">Save and finish</button>
</form>
Within my Routes, I have set everything up appropriately:
Route::get('/assess/{room_name}/{subject_selected}/{unit_selected}/{outcome_selected}', 'AssessmentHandler#step_6_assess');
Route::post('/assess//{room_name}/{subject_selected}/{unit_selected}/{outcome_selected}', 'AssessmentHandler#step_6_save');
The functions within the Controller are defined and everything should be working fine.
However, when I submit the form I am getting this error message:
Symfony \ Component \ HttpKernel \ Exception \
MethodNotAllowedHttpException No message
I have not seen this message before and am not sure where this is being triggered from in my particular instance. All of the search results I've read about this specific error talk about changing from GET to POST or ensuring CSRF protection is in place, but that isn't my issue here.
Using Dev tools, it is showing a 405 error (Method Not Allowed).
Any idea what might be happening here?

Try replacing {{ csrf_field() }} with #csrf.
You also have 2 slashes in the post url: Route::post('/assess//{room_name}
Make sure you are redirecting to the correct page.
Maybe you could show us the code in your controller.
Is your controller really AssessmentHandler and not AssesmentHandlerController?

Related

After rewriting some of my routes using put, patch and delete my functionality doesn't work anymore and the page just refreshes. Not getting errors

After finding out I've been writing my routes slightly wrong I decided to fix them. The routes originally looked like this:
Route::get('/deleteImage/{id}', 'ArtworkController#deleteImage')->name('deleteImage');
Route::get('/deleteCategory/{id}', 'CategoryController#deleteCategory')->name('deleteCategory');
Route::patch('/profile/{id}/update', 'UsersController#updateProfile')->name('updateProfile');
And I changed them to:
Route::delete('/image/{id}', 'ArtworkController#deleteImage')->name('deleteImage');
Route::delete('/category/{id}', 'CategoryController#deleteCategory')->name('deleteCategory');
Route::patch('/profile/{id}', 'UsersController#updateProfile')->name('updateProfile');
Sadly, as soon as I changed Route::get('/deleteImage/{id}' to Route::delete('/image/{id}' something went wrong.
This is the <a> tag that leads to the deleteImage route:
<a class='placeholderDelete' href='{{ route('deleteImage', ['image_id' => $image->id]) }}'>Delete Image</a>
Before making the changes the image would get deleted after clicking that anchor element, however, after the changes the page just refreshes and nothing happens. The image doesn't get deleted from my database and storage.
I'm not sure how to troubleshoot as I'm not getting any errors or anything like that.
DELETE routes need to be handled by a form that uses POST. Normal links (anchor tags) visit a page using a GET request. You will need to use a form. Here is an example.
<form id="delete-form" class="form" role="form" method="POST" action="{{route('deleteImage', $image->id)}}">
{{ csrf_field() }}
<input name="_method" type="hidden" value="DELETE">
<button type="submit">Delete</button>
</form>
If you want to use a regular link to do the deletion, you could hide the form and use javascript to submit it.
Delete Image

DELETE request on a link in laravel 5.4

I am trying to hit a route I made to make a delete HTTP request in laravel view when user clicks on 'Delete' button, but it won't work. I've read it should be done with forms in laravel.
Here is my code:
<form action="/admin/pages/delete/{{ $section->id }}" method="post">
{{ method_field('delete') }}
<button class="btn btn-sm" type="submit">Delete</button>
</form>
What is a proper way to handle this?
It shows me an error in the console, Bootbox: 'please specify a message' whenever I click on the button.
Route definition inside admin group:
Route::delete('/pages/delete/{id}', 'PagesController#delete')->name('pages.delete');
I believe you are missing the csrf token in the form.
You can add
{{ csrf_field() }}
just after your form starts.
Visit this link for knowing more about csrf
You must add the the CSRF Field because all form submission must past through the VerifyCsrfToken middleware before the request be procede by the controller
{{ csrf_field() }} // add this before or after the {{ method_field() }}

Laravel 5.4 "MethodNotAllowedHttpException" (Basic Task List)

i was trying to follow this tutorial on Laravel, even though most of the code & directories there were outdated, i managed to make the "Add Task" and also The Form to show up after a few errors.
now the problem, the delete button.
when i click on it, it shows a "MethodNotAllowedHttpException".
i changed the source code to match the newest version of Laravel.
my form (current version) :
<form action="{{ url('/task/'.$task->id) }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-danger">
<i class="fa fa-btn fa-trash"></i>Delete
</button>
</form>
my route :
Route::delete('/task/{id}', function ($id) {
Task::findOrFail($id)->delete();
return redirect('/');
});
i've been trying to fix this for 4 hours now, changing the methods of my route and form; but to no avail.
this is my first question on this site, sorry if there's something wrong in this question.
thanks~
edit:
to further help the effort, here's the complete error log
Error log, in Google Chrome
Change
<form action="{{ url('task/'.$task->id) }}" method="DELETE">
to
<form action="{{ url('task/'.$task->id) }}" method="POST">
Because form method DELETE does not exist, Laravel just "overwrites" this method with the hidden input "method" (you can place this input using "{{ method_field('DELETE') }}").
Form dosent support method Delete or Put ... It support only get and post methods, if You want implement delete in laravel this article will help you
link
Answer :
Turns out the problem lies in the Database-table itself, let me explain :
i was referring to a 'tasks' table that is referred as 'task' in code (no problem)
BUT, i was referring to a column called "ID" in my table as "id" in my code, creating the error (a rookie mistake).
thanks to #Autista_z for the pointer, and everyone else below for the guidance!

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.

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>

Categories