使用表单提交删除laravel中的多个记录

时间:2019-03-28 10:08:49

标签: php laravel

您好,我需要删除由多个复选框选中的多个用户记录,然后按Delete键并提交表单userListform

  1. 在视图中,我使用javascript deleteUser()添加了锚点
<a href="javascript:void();" class="btn btn-outline-danger" onclick="deleteUser();">Delete</a>
  1. 视图中
<form method="POST" action="{{url('/user/delete')}}" id="userListform" >
  1. 在路线
Route::post('/user/delete', 'UsersController@delete');
  1. 删除功能

function deleteUser(){    
    var $checkboxes = jQuery('#userListform input[type="checkbox"]');
    checkedItems = $checkboxes.filter(':checked').length;    
    if(checkedItems > 0){
        var status = confirm("Do you need to delete the user!!");   
        if (status == true) {                        
            document.getElementById("userListform").submit(); 
        } else {
            return false;
        }
    }else{
        alert("Please select the users to be deleted!");    
    }
}

我已经成功提交了表单,但是我没有使用usercontroller@delete方法的删除功能,而是显示404错误

2 个答案:

答案 0 :(得分:2)

以表格形式添加csrf令牌,它将起作用

答案 1 :(得分:0)

您可以尝试

in the view
<form method="POST" action="{{route('user.delete')}}" id="userListform" >

in the route
Route::post('/user/delete', 'UsersController@delete')->name('user.delete);