Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DELETE request in laravel

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

like image 259
lun7code Avatar asked Dec 02 '22 10:12

lun7code


1 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();
}
like image 150
num8er Avatar answered Dec 22 '22 05:12

num8er