Laravel - 在用户登录时删除 auth 用户帐户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25191102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:55:15  来源:igfitidea点击:

Laravel - Deleting auth user account while user is logged in

laravellaravel-4user-accounts

提问by Hyman Barham

I'm trying to build a function where a user can delete their own account while they are logged in. I'm struggling to find any example or logic/best practices.

我正在尝试构建一个功能,让用户可以在登录时删除自己的帐户。我正在努力寻找任何示例或逻辑/最佳实践。

The controller looks like this:

控制器看起来像这样:

public function postDestroy() {

        $user = User::find(Auth::user()->id);
        $user = DB::delete('delete from users')->user(id);
        return Redirect::route('site-home')->with('global', 'Your account has been deleted!');

}

I'm trying to grab the current Auth (logged in) user and use their id to delete them from the database. Then send them to the home page with a message.

我正在尝试获取当前的 Auth(登录)用户并使用他们的 ID 从数据库中删除它们。然后通过消息将它们发送到主页。

Also, do I need to make sure the session is properly closed during this process, such as Auth::logout(); ?

另外,我是否需要确保在此过程中正确关闭会话,例如 Auth::logout(); ?

I'm pretty new to Laravel, so any help would be appreciated.

我对 Laravel 还很陌生,所以任何帮助将不胜感激。

回答by user3657823

Not sure how your routing looks like, but this should do the job.

不确定您的路由是什么样的,但这应该可以完成工作。

    $user = \User::find(Auth::user()->id);

    Auth::logout();

    if ($user->delete()) {

         return Redirect::route('site-home')->with('global', 'Your account has been deleted!');
    }

You should logout user before delete.

您应该在删除之前注销用户。