我在路线中定义了一个资源:
Route::resource('shops', 'shopsController');
我需要创建一个删除项目的链接
<a class="btn btn-xs btn-danger" href="{{ URL::to('shops/'. $row->id . '/destroy') }}" > </a>
但是这个网址要求我定义一条新的路线:
Route::get('shops/{id}/destroy', 'shopsController@destroy');
那么,如何通过其资源强制URL通过默认路由来获取destroy函数?
我试过
href="{{ route('shops.destroy', $row->id ) }}" data-method="delete"
但是我将我重定向到show()而不是!!!!
答案 0 :(得分:3)
您无法通过锚标记href attr删除用户,您需要使用表单或使用Ajax删除它。
{{ Form::open(array('url' => 'shops/'. $row->id)) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete this User', array('class' => 'btn btn-warning')) }}
{{ Form::close() }}
这里是 similar question
<强>更新强>
希望这会对你有所帮助。
您可以将$ row-&gt; id传递给锚点ID attr,如此
<a class="btn btn-xs btn-danger" id="{{$row->Id}}" onclick="delete_user(this.id) return false;" href="#" rel="nofollow" >Delete this entry</a>
然后使用以下脚本处理Destory方法。
function delete_user(Id) {
var result = confirm("Want to delete?");
if (result==true) {
$.ajax({
url:'http://localhost:81/laravel/myapps/public/shops/'+Id,
type:"Post",
data: {'_method':'delete'},
success:function($msg){
alert($msg);
}
});
}
}
</script>
答案 1 :(得分:1)
试试这个
<a class="btn btn-xs btn-danger" href="{{ route('shops.destroy',array($row->id)) }}" data-method="delete" rel="nofollow" data-confirm="Are you sure you want to delete this?">Delete this entry</a>