我正在尝试删除 Laravel 中的类别
在我的 Controller 中我有这个
public function destroy(Category $category)
{
$category->delete();
return redirect()->back()
->with('success','Category deleted successfully');
}
我也尝试在我的 Controller 中执行此操作
public function destroy($id)
{
Category::destroy($id);
return redirect()->back()
->with('success','Category deleted successfully');
}
在我看来,我有这个
<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>
在我的 web.php 中,我有这个
Route::delete('/admin/category/{category},'CategoriesController@destroy');
它不断将我重定向到
404 Not Found
并且它没有删除 拜托,我不知道我做错了什么
最佳答案
这是因为您的表单操作正在呈现错误的网址,而您的 web.php
文件中没有该网址。呈现的网址类似于 http://127.0.0.1:8000/admin/category/%7Bcategory-%3Eid%7D
因为您使用的是 {category->id}
而不是类别变量。
表单操作应该是这样的
<form action="admin/category/{{$category->id}}" method="POST">
更好的方法是使用命名路由
Route::delete('admin/category/{category}','CategoriesController@destroy')->name('category.destroy');
和表单操作
<form action="{{ route('category.destroy',$category->id) }}" method="POST">
关于laravel - Laravel 中删除返回 404 Not Found 并没有删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59441629/