使用Laravel删除后如何注销用户? [英] How can I logout user after delete with Laravel?

查看:57
本文介绍了使用Laravel删除后如何注销用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel中删除用户数据后,注销用户的正确方法是什么?如果删除过程出现错误,我不想删除他.

What is the correct way to logout user after I delete his data in Laravel? I would not like to delete him before, in case of delete process goes with errors.

当我有此代码时:

    if($this->userManipulator->softDeleteUser(Auth::user())){
        Auth::logout();
        return redirect(url('login'));
    }

它在应用程序中工作正常,但在测试过程中无法正常工作.

it works fine in the app, but does not work correctly during testing.

推荐答案

正如我在评论中提到的,您必须先将用户从应用程序中注销,因为一旦删除,Eloquent将无法找到/注销用户.

As I mentioned in the comments, you must log the user out of your application first since once deleted Eloquent won't be able to locate/logout the user.

以下是一种解决方案,可以解决您担心 delete 失败时的处理方法.根据您的设置方式,可能需要进行调整,但是 这个概念会起作用:

Below is a solution that addresses your concern about what to do if the delete fails. It might need adjustment depending on how you have things setup, but this concept will work:

// Get the user
$user = Auth::user();

// Log the user out
Auth::logout();

// Delete the user (note that softDeleteUser() should return a boolean for below)
$deleted = $this->userManipulator->softDeleteUser($user);

if ($deleted) {
    // User was deleted successfully, redirect to login
    return redirect(url('login'));
} else {
    // User was NOT deleted successfully, so log them back into your application! Could also use: Auth::loginUsingId($user->id);
    Auth::login($user);

    // Redirect them back with some data letting them know it failed (or handle however you need depending on your setup)
    return back()->with('status', 'Failed to delete your profile');
}

这篇关于使用Laravel删除后如何注销用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆