Reputation: 719
I am trying to delete a category in Laravel
In my controller I have this
public function destroy(Category $category)
{
$category->delete();
return redirect()->back()
->with('success','Category deleted successfully');
}
I also tried to do this in my controller
public function destroy($id)
{
Category::destroy($id);
return redirect()->back()
->with('success','Category deleted successfully');
}
In my view, I have this
<div class="card card-default">
<div class="card-header">Category</div>
<div class="card-body">
<table class="table">
<thead>
<th>Name</th>
</thead>
<tbody>
@foreach($categories as $category)
<tr>
<td> {{ $category -> title }} </td>
<td>
<form action="admin/category/{{category->id}}" method="POST">
{{ method_field('DELETE') }}
@csrf
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
In my web.php, I have this
Route::delete('/admin/category/{category},'CategoriesController@destroy');
It keeps redirecting me to
404 Not Found
and it not deleting Please, I don't know what I am doing wrong
Upvotes: 2
Views: 6599
Reputation: 6233
it's because your form action is rendering wrong url which you don't have in your web.php
file. the rendered url is something like http://127.0.0.1:8000/admin/category/%7Bcategory-%3Eid%7D
because you are using {category->id}
instead of category variable.
the form action should be like this
<form action="admin/category/{{$category->id}}" method="POST">
better approach would be using a named route
Route::delete('admin/category/{category}','CategoriesController@destroy')->name('category.destroy');
and the form action
<form action="{{ route('category.destroy',$category->id) }}" method="POST">
Upvotes: 5