Reputation: 199
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.
Upvotes: 4
Views: 10183
Reputation: 19372
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
Upvotes: 9
Reputation: 35337
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.
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) {
}
Upvotes: 1
Reputation: 574
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.
Upvotes: 3
Reputation: 5629
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
Upvotes: 7