Laravel 5.5使用模式删除数据

时间:2018-07-12 13:11:58

标签: javascript php laravel

我想使用模式删除数据,我已经获得了要删除的数据的ID,但是当我单击模式中的删除按钮时,什么也没有发生。 请告诉我是否有问题。 非常感谢

这是我的控制器

 public function destroy(Request $request)
{
    $applicant = Applicant::where('fk_user_details_id', request('user_detail_id'))->delete();
    $userDetail = UserDetail::where('fk_users_id', request('id'))->delete();
    User::destroy(request('id'));

    return redirect('/applicant_list')->with('success', 'Applicant Removed');
}

这是情态的

<form action="{{route('applicant_delete', 'delete')}}" method="POST" class="remove-record-model">
{{ method_field('delete') }}
{{ csrf_field() }}
<div id="applicantDeleteModal" class="modal modal-danger fade" tabindex="-1" role="dialog" aria-labelledby="custom-width-modalLabel" aria-hidden="true" style="display: none;">
    <div class="modal-dialog" style="width:55%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title text-center" id="custom-width-modalLabel">Delete Applicant Record</h4>
            </div>
            <div class="modal-body">
                <h4>You Want You Sure Delete This Record?</h4>
                <input type="hidden", name="applicant_id" id="app_id" value="">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-danger waves-effect remove-data-from-delete-form">Delete</button>
            </div>
        </div>
    </div>
</div>
</form>

这是我的路线:

Route::delete('applicant_delete_modal/{applicants}', 'ApplicantController@destroy')->name('applicant_delete');

这是我的JavaScript

$('#applicantDeleteModal').on('show.bs.modal', function(e) {
    var $invoker = $(e.relatedTarget);
    var $id = $invoker.attr('data-id');
    var data = $('#data_applicant-' + $id).html();

    data = JSON.parse(data);
    console.log(data);
    $('#app_id').val(data.applicants_id);

})

3 个答案:

答案 0 :(得分:0)

您的按钮必须是Submit类型,才能提交表单。

<button type="submit" class="btn btn-danger waves-effect remove-data-from-delete-form">Delete</button>
<!--          ^ Here    -->

答案 1 :(得分:0)

您的路线:

Route::delete('applicant_delete_modal', 'ApplicantController@destroy')->name('applicant_delete');

在您的控制器中:

 public function destroy(Request $request)
{ 
    $applicant_id=$request->input('applicant_id');

    $applicant = Applicant::where('fk_user_details_id',$applicant_id)->delete();
    $userDetail = UserDetail::where('fk_users_id',$applicant_id)->delete();
    User::destroy($applicant_id);
    return redirect('/applicant_list')->with('success', 'Applicant Removed');
}

您的模式应为:

<div id="applicantDeleteModal" class="modal modal-danger fade" tabindex="-1" role="dialog" aria-labelledby="custom-width-modalLabel" aria-hidden="true" style="display: none;">
    <div class="modal-dialog" style="width:55%;">
        <div class="modal-content">
             <form action="{{route('applicant_delete')}}" method="POST" class="remove-record-model">
               {{ method_field('delete') }}
               {{ csrf_field() }}

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title text-center" id="custom-width-modalLabel">Delete Applicant Record</h4>
            </div>
            <div class="modal-body">
                <h4>You Want You Sure Delete This Record?</h4>
                <input type="hidden", name="applicant_id" id="app_id">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-danger waves-effect remove-data-from-delete-form">Delete</button>
            </div>

             </form>
        </div>
    </div>
</div>

在点击删除按钮时以模式设置数据:

假设您要在table中显示数据,然后 给属性data-userid删除按钮。

@foreach($users as $user)
<tr>
<td>{{$user->name}}</td>
<td><button class="btn btn-danger deleteUser" data-userid="{{$user->id}}">Delete</button></td>  
</tr>
@endforeach

现在,当用户单击deleteUser按钮类javascript

时,我们将数据设置为模式并显示模式
<script>
$(document).on('click','.deleteUser',function(){
    var userID=$(this).attr('data-userid');
    $('#app_id').val(userID); 
    $('#applicantDeleteModal').modal('show'); 
});
</script>

答案 2 :(得分:0)

我在Saurabh Mistry的回答中看到了您的评论 我认为您可能在where子句中输入了错误的列名

您可以尝试通过此操作检查查询是否返回正确的结果

dd(Applicant::where('fk_user_details_id',$applicant_id)->get());

如果它返回null或空集合,则可能必须重新检查where子句