繁体   English   中英

Laravel 5.5使用模式删除数据

[英]Laravel 5.5 Delete Data using modal

我想使用模式删除数据,我已经获得了要删除的数据的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);

})

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

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

您的路线:

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属性指定delete按钮。

@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>

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM