0

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

halfer
  • 19,824
  • 17
  • 99
  • 186
Kevin fu
  • 222
  • 1
  • 4
  • 16
  • 1
    You should use method="post" and then use the helper "method field" as you have already done – ExoticSeagull Mar 13 '17 at 09:31
  • @ClaudioLudovicoPanetta thanks for the fast response, but changing both from "delete" to "post" made the delete button behave like the "add task" button from the tutorial; as it's returning a validation error ("the name field is required") rather than actually deleting the 'task' from the Database. – Kevin fu Mar 13 '17 at 09:38
  • You dont need `{{method_field('delete')}}` when you are already passing the hidden field of `_method` – z1haze Mar 13 '17 at 14:10
  • @StephenHendricks thank you for the comment, i excluded out the "method_field", and also "_method" interchangeably, choosing one or the other; but still not fixed. – Kevin fu Mar 14 '17 at 01:32
  • We don't do [solved] edits here, would you add a self-answer instead? Thanks! – halfer Mar 14 '17 at 23:53

3 Answers3

4

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') }}").

skm
  • 1,192
  • 1
  • 11
  • 23
Autista_z
  • 2,491
  • 15
  • 25
  • thanks for the answer, i tried changing the method of the form from "DELETE" to "POST", and it returned the same 'MethodNotAllowedHttpException" error; when i click on the "Delete Button" – Kevin fu Mar 13 '17 at 09:43
  • @Kevinfu could you check if the form action url is correct? is the helper function url() creating correct url? – Autista_z Mar 13 '17 at 10:14
  • sorry, but I'm new to Laravel and web development in general; and i believe that the URL that i entered is correct (i followed the tutorial). – Kevin fu Mar 13 '17 at 11:24
  • Not exactly, in the tutorial is url vithout this function and with "/" common slash in the begining. If thats not the problem. Because everithing else is ok. – Autista_z Mar 13 '17 at 12:31
  • Thank you soo much, after reading your comment and though of the possibility that it was the URL (because the methods used are all right), i went to the Database and checked the table turns out the field "id" doesn't exist; instead it was "ID", after changing the column name in my database-table; it worked! thanks mate! :D – Kevin fu Mar 14 '17 at 03:36
0

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

Community
  • 1
  • 1
Vahe Galstyan
  • 1,681
  • 1
  • 12
  • 25
  • i tried method spoofing "Delete" into the form, still returns the same error sadly... – Kevin fu Mar 13 '17 at 10:04
  • turns out it was the URL, and how my database-table was set-up. thanks for the help anyways, learned a lot from reading your link! – Kevin fu Mar 14 '17 at 03:37
0

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!

Kevin fu
  • 222
  • 1
  • 4
  • 16