Daolin
Daolin

Reputation: 634

Laravel 5: delete user record using Route::delete getting MethodNotAllowedHttpException in RouteCollection.php

Try to do this on the page:

<form class="form-horizontal" role="form" method="DELETE" action="/user/{{ $user->id }}/delete">
      <button type="submit" class="btn btn-danger">
          Delete
      </button>
</form>

The route:

Route::delete('user/{id}/delete', ['middleware' => ['admin'], 
                                   'uses' => 'Auth\UserController@destroy']);

The controller:

class UserController extends Controller
{
    public function destroy($id)
    {
        DB::table('users')->where('id', $id)->delete();
        return view('admin/dash');
    }
}

I'm getting MethodNotAllowedHttpException in RouteCollection.php. How do I fix it?

Solution:

Thanks to Josh. I solve it by changing form to

<form method="POST" action="/user/{{ $user->id }}/delete">
                            <input type="hidden" name="_token" value="{{ csrf_token() }}">
                            <input type="hidden" name="_method" value="DELETE" />
                            <button type="submit" class="btn btn-danger">
                                Delete
                            </button>
                        </form>

Upvotes: 3

Views: 2885

Answers (1)

Josh Rumbut
Josh Rumbut

Reputation: 2710

In some implementations of some versions of HTML, only GET and POST are allowed as methods.

You can overcome this by adding an addition attribute _method that you process yourself, or you can use JavaScript.

If you're using Chrome, check the Network tab in developer tools to verify either that this is the problem or add the request to your post for further diagnostics.

See here for more details.

Upvotes: 3

Related Questions