Laravel 6:在仍然登录的情况下删除用户

目前我正在开发一个网站。我正在为我的一项功能开发 CRUD,并且在删除点之前没有遇到任何问题。我使用 bootstrap 模式窗口作为弹出的警告窗口,以阐明您希望删除您的d()}}

                <div class="form-group">

                    <label for="name">Account Email:</label>

                    <input type="text" name ="email" value="{{$user -> email}}" class="form-control" readonly>

                    <div class="form-group">

                    <div class="text-centre">

                    <p></p>

                    <button type="button" data_userid="{{$user->id}}" class="btn btn-danger" data-toggle="modal" data-target="#deleteModal">

                        Delete

                    </button>

                    </div>

                    </form>

                </div>

                </div>

            </div>

        </div>

    </div>

</div>


<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">

  <div class="modal-dialog" role="document">

    <div class="modal-content">

      <div class="modal-header">

        <h5 class="modal-title" id="deleteModalLabel">Are you sure?</h5>

        <form action="{{ route('delete', $user)}} " method="post">

        {{method_field('delete')}}

        {{csrf_field()}}

        <button type="button" class="close" data-dismiss="modal" aria-label="Close">

          <span aria-hidden="true">&times;</span>

        </button>

      </div>


      <div class="modal-body">

        Are you sure you want to permanetly delete your account?

      </div>

      <div class="modal-footer">

        <button type="button" class="btn btn-primary" data-dismiss="modal">No, cancel</button>

        <button type="submit" class="btn btn-danger">Yes, delete my account</button>

      </div>

      </form>

    </div>

  </div>

</div>


另外,每当我尝试从带有路由的下拉菜单访问页面时


 <a class="dropdown-item" href="{{ route('users.destroy', $user)}}">

                                        Delete Account

我遇到了一个空白页面,我确信这取决于参数。所以澄清一下,每当模式窗口弹出并且用户点击“是,删除我的帐户”时,什么都没有发生,窗口保持打开状态,我到页面的路由 {{ route('users.destroy', $user)}} 会导致空白页面。任何帮助是极大的赞赏!


牛魔王的故事
浏览 141回答 4
4回答

斯蒂芬大帝

这就是我们尝试删除 Laravel 的方式public function destroy(User $user){&nbsp;&nbsp;&nbsp; &nbsp; $user->delete();&nbsp; &nbsp; return Redirect::route('home')->with('global', 'Your account has been deleted!');&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}

森栏

改变<form&nbsp;action="{{&nbsp;route('delete',&nbsp;$user)}}&nbsp;"&nbsp;method="post">至<form&nbsp;action="{{&nbsp;route('delete',&nbsp;['user'&nbsp;=>&nbsp;$user])&nbsp;}}"&nbsp;method="post">在模态。还<a&nbsp;class="dropdown-item"&nbsp;href="{{&nbsp;route('users.destroy',&nbsp;$user)}}">Delete&nbsp;Account&nbsp;</a>不起作用,因为它是 GET 请求。删除应该是一个 DELETE 请求。所以你应该点击打开模态;<a&nbsp;href="#"&nbsp;data-toggle="modal"&nbsp;data-target="#deleteModal"&nbsp;class="dropdown-item">&nbsp;Delete&nbsp;Account</a>

料青山看我应如是

根据您的配置,您似乎为表单和锚点使用了错误的别名。你应该切换它们。这是你的路线配置:Route::get('/users/delete',&nbsp;'[...]')->name('delete');&nbsp; Route::delete('admin/users/{user}',&nbsp;'[...]')->name('users.destroy');这是你应该如何使用它们:你<a/>应该使用route('delete')你<form/>应该使用route('users.destroy', ['user' => $userId]).您的删除操作应如下所示:删除用户注销当前用户重定向到某处&nbsp; public function destroy($id)&nbsp; {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; User::find($id)->delete();&nbsp; &nbsp; &nbsp; Auth::logout();&nbsp; &nbsp; &nbsp; return Redirect::route('home')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->with('global', 'Your account has been deleted!');&nbsp; }希望这可以帮助。

慕尼黑5688855

你可以使用 ajax 和 sweetalert 来做到这一点,它对 ui 更具交互性在你的刀片文件中@foreach($user as $u)&nbsp; &nbsp;<a href="javascript:void(0);" classs="delete" data-id="{{$u->id}}">Delete</a>@endforeach在你的刀片底部你可以调用ajax请求<script>&nbsp; $(document).on('click','.delete',function()&nbsp; {&nbsp; &nbsp; var id = $(this).data('id');&nbsp; &nbsp; swal({&nbsp; &nbsp; &nbsp; &nbsp; title: 'Are you sure you want to delete this?',&nbsp; &nbsp; &nbsp; &nbsp; type: "warning",&nbsp; &nbsp; &nbsp; &nbsp; showCancelButton: true,&nbsp; &nbsp; &nbsp; &nbsp; confirmButtonClass: "btn btn-danger m-btn m-btn--pill m-btn--icon m-btn--air",&nbsp; &nbsp; &nbsp; &nbsp; confirmButtonText: '<span><i class="la la-thumbs-up"></i> Yes, Delete it!</span>',&nbsp; &nbsp; &nbsp; &nbsp; cancelButtonClass: 'btn btn-secondary m-btn m-btn--pill m-btn--icon m-btn--air',&nbsp; &nbsp; &nbsp; &nbsp; cancelButtonText: '<span><i class="la la-thumbs-down"></i>No, thanks</span>',&nbsp; &nbsp; }).then(function(e){&nbsp; &nbsp; &nbsp; &nbsp; if(e.value){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url:"{{route('admin.document.delete')}}",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type:'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers:{ 'X-CSRF-Token' : jQuery('meta[name=csrf-token]').attr('content') },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType:'json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data:{'id':id,_token: '{{csrf_token()}}'},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success:function(response){&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var msg = response.msg;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(response.status=='success'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //here you can do whatever after delete msg&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //for reload u can use - (location.reload);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });});&nbsp;</script>最后在你的控制器中public function destroy(Request $request){&nbsp; &nbsp;$id=$request['id'];&nbsp; &nbsp;$delete = manageMultipleDocument::find($id)->delete();//optional&nbsp; &nbsp;$flashArr = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'msg' => 'Document deleted successfully.',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'status' => 'success'&nbsp; &nbsp; &nbsp; &nbsp; );//optional&nbsp; &nbsp;$request->Session()->flash('succ_message',$flashArr);//Required return any thing&nbsp; &nbsp;return $flashArr;}
打开App,查看更多内容
随时随地看视频慕课网APP