在Laravel 5.8或更高版本中使用Auth :: user()-> user()

在Laravel 5.8或更高版本中使用Auth :: user()-> user()

问题描述:

我正在尝试将Laravel项目从Laravel 5.1升级到5.8.我注意到在项目中使用了Auth::user()->user()而不是Auth::user(),并且在Laravel 5.8中,由于Auth::user为null,在某些情况下我得到了null异常.我应该用Auth::user()替换Auth::user()->user()的所有实例吗?

I am trying to upgrade a Laravel project from Laravel 5.1 to 5.8. I noticed that instead of Auth::user(), Auth::user()->user() has been used in the project, and in Laravel 5.8 I get a null exception in some cases because Auth::user is null. Shall I replace all instances of Auth::user()->user() with Auth::user()?

您应该使用此-

Auth::user();

或者这个

auth()->user();

请确保您的用户模型中没有名称为 user()的方法/关系,这可能是您拥有 user()-> user()的原因>类似语法.如果不是这种情况,请随时将其替换为 auth()-> user()

Please make sure that you have no method/relation with name user() in your user model which may be the reason you have user()->user() like syntax. If that's not the case then feel free to replace it with auth()->user()