laravel 5.4 MethodNotAllowedHttpException in RouteCollection.php (line 251)

I’m an amateur in laravel. I use laravel 5.4. so I want to make process delete without form binding but I have an error message like this. Please tell me how to solving this.

route:

Route::delete('test/{id}','<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cd8e9fff8cfe3e2f8fee3e0e0e9fecce8e9fff8fee3f5">[email protected]</a>');

My Form:
<td><button type="button" class="btn"><a href="{{URL::to('coba/test/'.$post->id.'/edit') }}" rel="nofollow noreferrer noopener" >Edit</a></button><button type="button" class="btn"><a href="{{ action('<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4317263037002c2d37312c2f2f26310327263037312c3a">[email protected]</a>', $post['id']) }}" rel="nofollow noreferrer noopener" method="post" >Hapus</a></button>{{ csrf_field() }}{{ method_field('DELETE') }}
    </td>

My Controller:
public function destroy($id)
{
   $post = Post::find($id);
   $post->delete();
   return redirect()->to('coba/test');`
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Href on an anchor html element will result in a GET call but your route expect a Delete call. You have some ways to make sure you will result in a delete call.

One of the most common ways is to use a form instead to post data to your server.

Delete

    {{ Form::open(['url' => 'test/'.$post->id, 'method' => 'DELETE']) }}
    {{ Form::button('delete', ['type' => 'submit', 
                               'class' => 'btn']) }}
    {{ Form::close() }}

Edit
    {{ Form::open(['url' => 'coba/test/'.$post->id.'/edit', 'method' => 'POST']) }}
    {{ Form::button('delete', ['type' => 'submit', 
                               'class' => 'btn']) }}
    {{ Form::close() }}

For best practise I recommend to use {{ Form::open(...) }} {{ Form::close() }} only once and refactor your controller code so it can read the value from the buttons and translate that in the corresponding id of the post so you dont have multiple html forms in your code.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x