Laravel 销毁选项不删除
Laravel Destroy Option not deleting
为什么会出现这种情况?这是我所有帐户的索引列表。我只想通过 category.destroy 路线删除特定类别,但它是
index.blade.php
@extends('layouts.master')
@section('title','All Categories')
@section('contents')
<div class="row">
<div class="col-md-8 col-sm-4 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">All Categories</div>
<div class="panel-body">
<article>
<div class="table-responsive-vertical shadow-z-1">
<!-- Table starts here -->
<table id="table" class="table table-hover table-mc-light-blue">
<thead>
<tr>
<th>ID No</th>
<th>Category</th>
<th>Edit/Delete</th>
<th>Status</th>
</tr>
</thead>
@foreach($categories as $category)
<tbody>
<tr>
<td data-title="ID">{{$category->id}}</td>
<td data-title="Name">{{$category->name}}</td>
<td><a href="{{ route('category.edit',$category->id) }}" class="btn btn-primary btn-sm pull-left">Edit</a>
 <a href="{{ route('category.destroy', $category->id) }}" class="btn btn-danger btn-sm">Delete</a>
</td>
</tr>
</tbody>
@endforeach
</table>
</div>
</article>
</div>
</div>
</div>
</div>
@endsection
@section('js')
{!!Html::script('assets/js/jquery.min.js')!!}
{!!Html::script('assets/js/bootstrap.min.js') !!}
<script>
$('#flash-overlay-modal').modal();
</script>
<script>
$('div.alert').not('.alert-important').delay(3000).fadeOut(350);
</script>
@endsection
CategoryController.php
public function destroy($id){
$category = Category::findOrFail($id);
$category->delete();
Session::flash('flash_message', 'Task successfully deleted!');
return redirect()->route('category.index');
}
相反,它只显示该类别的视图特定条目。不是删除什么的
试试这条路线:
Route::get('category/{category}/destroy',[
'uses'=>'CategoryController@destroy',
'as' => 'category.destroy'
]);
要访问您的销毁路由,您必须使用 DELETE HTTP 请求动词。 HTML links 只允许 GET 请求。
您应该将 HTML link 更改为 HTML 形式,这样 spoofs the DELETE method, or look into using something like restfulizer.js 会自动将您的删除 link 转换为删除形式。
正如所建议的那样,您也可以为删除功能创建一个 GET 路由,但这会产生潜在的后果。 GET 和 HEAD 请求一般应被视为 "read only" 请求,不应修改任何数据。 POST、PUT、PATCH 和 DELETE 请求通常被认为是 "write" 请求。网络蜘蛛可能会抓取您的 delete link 并最终删除您的所有数据,或者网络浏览器可能会预取页面上的所有 GET 请求,因此即使没有访问 delete link一个点击了删除按钮。当您开始允许 GET 请求修改数据时,可能会发生很多潜在的麻烦事。 this answer.
中有一些很好的信息
为什么会出现这种情况?这是我所有帐户的索引列表。我只想通过 category.destroy 路线删除特定类别,但它是
index.blade.php
@extends('layouts.master')
@section('title','All Categories')
@section('contents')
<div class="row">
<div class="col-md-8 col-sm-4 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">All Categories</div>
<div class="panel-body">
<article>
<div class="table-responsive-vertical shadow-z-1">
<!-- Table starts here -->
<table id="table" class="table table-hover table-mc-light-blue">
<thead>
<tr>
<th>ID No</th>
<th>Category</th>
<th>Edit/Delete</th>
<th>Status</th>
</tr>
</thead>
@foreach($categories as $category)
<tbody>
<tr>
<td data-title="ID">{{$category->id}}</td>
<td data-title="Name">{{$category->name}}</td>
<td><a href="{{ route('category.edit',$category->id) }}" class="btn btn-primary btn-sm pull-left">Edit</a>
 <a href="{{ route('category.destroy', $category->id) }}" class="btn btn-danger btn-sm">Delete</a>
</td>
</tr>
</tbody>
@endforeach
</table>
</div>
</article>
</div>
</div>
</div>
</div>
@endsection
@section('js')
{!!Html::script('assets/js/jquery.min.js')!!}
{!!Html::script('assets/js/bootstrap.min.js') !!}
<script>
$('#flash-overlay-modal').modal();
</script>
<script>
$('div.alert').not('.alert-important').delay(3000).fadeOut(350);
</script>
@endsection
CategoryController.php
public function destroy($id){
$category = Category::findOrFail($id);
$category->delete();
Session::flash('flash_message', 'Task successfully deleted!');
return redirect()->route('category.index');
}
相反,它只显示该类别的视图特定条目。不是删除什么的
试试这条路线:
Route::get('category/{category}/destroy',[
'uses'=>'CategoryController@destroy',
'as' => 'category.destroy'
]);
要访问您的销毁路由,您必须使用 DELETE HTTP 请求动词。 HTML links 只允许 GET 请求。
您应该将 HTML link 更改为 HTML 形式,这样 spoofs the DELETE method, or look into using something like restfulizer.js 会自动将您的删除 link 转换为删除形式。
正如所建议的那样,您也可以为删除功能创建一个 GET 路由,但这会产生潜在的后果。 GET 和 HEAD 请求一般应被视为 "read only" 请求,不应修改任何数据。 POST、PUT、PATCH 和 DELETE 请求通常被认为是 "write" 请求。网络蜘蛛可能会抓取您的 delete link 并最终删除您的所有数据,或者网络浏览器可能会预取页面上的所有 GET 请求,因此即使没有访问 delete link一个点击了删除按钮。当您开始允许 GET 请求修改数据时,可能会发生很多潜在的麻烦事。 this answer.
中有一些很好的信息