1
votes

I am trying to use Laravel's (5.3) destroy method to delete an object when I push a button, however I'm getting a MethodNotAllowedHttpException.

My route is

Route::delete('/admin/add/{id}', 'ItemController@destroy');

The destroy method is

public function destroy($id)
{
    $items = Item::where('id', $id)->get();
    foreach ($items as $item)
        $item->delete();
    return redirect('/admin/add');
}

My form is

<form action="{{ url('admin/add/'.$specific->id)}}" method="POST">
  {{ method_field('DELETE') }}
  {{ csrf_field() }}
  <div class="modal-footer no-border">
    <button type="submit" class="btn btn-primary">Delete</button>
  </div>
</form>

I checked the existing routes (php artisan route:list), and the delete one is showing:

DELETE   | admin/add/{id}   |          | App\Http\Controllers\ItemController@destroy

The error that I am getting is

in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'POST')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD', 'POST')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 766
at Router->findRoute(object(Request)) in Router.php line 621
at Router->dispatchToRoute(object(Request)) in Router.php line 607
at Router->dispatch(object(Request)) in Kernel.php line 268
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 150
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 117
at Kernel->handle(object(Request)) in index.php line 53
at require('/Users/username/Sites/blades/public/index.php') in server.php line 133

Per the Laravel routing instructions, I'm using a form and spoofing the DELETE action using a POST method, but I'm still getting an exception. Any help would be appreciated.

NOTE: I followed the full example at https://laravel.com/docs/5.2/quickstart-intermediate, and it works perfectly fine there. My attempts at recreating this in a different project is what's causing the issue.

2
Can you check the generated HTML? Looks like your form is sending back a POST request after all.Nandini Bhaduri
It does appear to be sending a POST request. I'm adding an update above because I found something rather strange.abe678

2 Answers

0
votes

Please check what $specific->id is not null, like dd($specific->id), after open developer console in you browser and open network page, and check what url is correct.

0
votes

I found the problem. The page that this form was on had a different POST form above it, and I had closed the form with a </div> rather than a </form> tag. Because of that, it was trying to submit both forms at the same time, and the POST calls were conflicting.