最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - DELETE request in laravel - Stack Overflow

programmeradmin2浏览0评论

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

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.

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

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.

Share Improve this question edited Nov 15, 2018 at 15:17 lun7code asked Nov 15, 2018 at 14:37 lun7codelun7code 1992 gold badges4 silver badges14 bronze badges 2
  • 1 Don't forget to use {!! method_field('delete') !!} – Peon Commented Nov 15, 2018 at 14:44
  • Also, your onClick validation will always work, if JS is disabled, meaning, it will delete by default and not delete, if JS confirm works. If you want to make a validation, make sure that by default it doesn't work, and will work ONLY if you successfully validate request. – Peon Commented Nov 15, 2018 at 14:47
Add a comment  | 

4 Answers 4

Reset to default 9

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

UPD.: This answer may be a bit outdated and Form class may be removed from new versions of Laravel. If you see: Class "Form" not found install laravelcollective\html package

composer require laravelcollective/html

or remove Form::token()

or replace with: csrf_token described here

MethodNotAllowedHttpException tells us that we tried to request the server with a http method type which is not supported at that endpoint. For example you tried to do a GET request on an url which only allows DELETE.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405

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) {

 }
发布评论

评论列表(0)

  1. 暂无评论