DELETE request in laravel

  • A+
Category:Languages

I have a categories page and create a remove button which to remove it the categories, the image as below: DELETE request in laravel

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. DELETE request in laravel

 


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(); } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: