在Laravel中删除他的数据后,注销用户的正确方法是什么? 我不想在删除之前删除他,以免删除过程中出现错误。
当我有这个代码时:
if($this->userManipulator->softDeleteUser(Auth::user())){ Auth::logout(); return redirect(url('login')); }它在应用程序中正常工作,但在测试过程中无法正常工作。
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.
When I am having this code:
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将无法找到/注销用户。
下面是一个解决方案,解决您在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'); }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.
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'); }更多推荐
发布评论