如何在使用Laravel删除后注销用户?(How can I logout user after delete with Laravel?)

系统教程 行业动态 更新时间:2024-06-14 17:02:17
如何在使用Laravel删除后注销用户?(How can I logout user after delete with Laravel?)

在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'); }

更多推荐

本文发布于:2023-04-21 18:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/cb21421646a9df9e66b460a7454d3643.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户   如何在   Laravel   delete   logout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!