DELETE request in laravel

Viewed 8679

I have a categories page and create a remove button which to remove it the categories, the image as below: enter image description here

So the problem is show me 'MethodNotAllowedHttpException'

Alright. Here is Route file

Route::delete('removeCategory/{id}','AdminController@removeCategory');

Controller file

 public function removeCategory(Request $id){
    $cats = cats::find($id);
    $cats->delete();

}

and View file

@foreach($data as $product)
  <tr  style="height:50px">
    <td style="padding:10px">{{$product->cat_name}}</td>
    <td><a class="btn btn-sm btn-fill btn-primary"
           href="{{url('/admin/editCategory')}}/{{$product->id}}">Edit</a></td>
    <td><a href="{{url('admin/removeCategory')}}/{{$product->id}}" onclick="return confirm('Are you sure?')"
       class="btn btn-sm btn-fill btn-primary">Remove</a></td>
  </tr>
@endforeach

Thanks for anyone share the info to me, I have tried it but showing me this error message. enter image description here

4 Answers

Since You want to do it without complicating the code with ajax,

solution is simply sending POST request and defining DELETE method as hidden field.

For simplicity You can add that field using method_field helper:

@foreach($data as $product)
  <tr  style="height:50px">
    <td style="padding:10px">{{$product->cat_name}}</td>
    <td><a class="btn btn-sm btn-fill btn-primary"
           href="{{url('/admin/editCategory')}}/{{$product->id}}">Edit</a></td>

    <td>
      <form 
        method="post" 
        action="{{url(''admin/removeCategory')}}/{{$product->id}}"> 

        {!! Form::token() !!}
        {{ method_field('DELETE') }}

        <button 
          type="submit"
          onclick="return confirm('Are you sure?')"
          class="btn btn-sm btn-fill btn-primary">Remove</button>
      </form>
    </td>

  </tr>
@endforeach

and make sure after deleting object it's returning back to listing:

public function removeCategory($id) {
  $Cat = cats::find($id);
  if ($Cat) {
    $Cat->delete();
  }
  return redirect()->back();
}

Try editing your route to this: Route::get('removeCategory/{id}','AdminController@removeCategory');

Then edit your controller to this:

public function removeCategory($id){
    $cats = cats::find($id);
    $cats->delete();
    return response(['Message' => 'This request has been deleted'], 200);
}

This is an alternative to the answer provided above, though I would recommend sticking to the answer provided by num8er.

  1. You must submit a DELETE request. Look at num8er's answer for this. You don't have to do it through a form, you can do it through AJAX, but just using <a href will result in a GET request.

  2. You are also type hinting $id as a Request object in the controller method. Therefore, Laravel will provide you with a Request object instead of the number passed as a parameter in the URL. You need to remove the type hinting on that parameter, or use a type that actually fits:


 public function removeCategory($id) {

 }
Related