在Laravel 5.8或更高版本中使用Auth :: user()-> user() [英] Using Auth::user()->user() in Laravel 5.8 or later

查看:233
本文介绍了在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()

这篇关于在Laravel 5.8或更高版本中使用Auth :: user()-> user()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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