在Laravel中删除数据后注销用户的正确方法是什么?我不想删除他之前,如果删除过程出错。
当我有这段代码时:
if($this->userManipulator->softDeleteUser(Auth::user())){
Auth::logout();
return redirect(url('login'));
}
它在应用程序中运行良好,但在测试期间无法正常工作。
答案 0 :(得分:0)
这是不可能的,Auth
无法找到它们,因为Eloquent会将其视为已删除。
解决方案:您应该在删除前注销用户。
$user = \User::find(Auth::user()->id);
Auth::logout();
if ($user->delete()) {
return Redirect::route('home');
}
答案 1 :(得分:0)
正如我在评论中提到的,您必须首先将用户从应用程序中删除,因为一旦删除,Eloquent将无法找到/注销用户。
以下解决方案可解决您在delete
失败时如何处理的问题。它可能需要调整,具体取决于您如何设置,但这个概念将工作:
// 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');
}