微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Laravel无法理解为什么尝试获取非对象的属性正在发生?

我有一个MembersController包含以下内容.

public function indexPaid()
{
    $this->middleware('auth');

    if(!Auth::user()->picked_tools)
    {
        if(Auth::user()->subscribedToPlan('one-tool',   'member')) return redirect('/pick-tools')->with('status', 'You have subscribed to use one of my products! Please select which product below you would like to use.');
        if(Auth::user()->subscribedToPlan('all-tools',  'member')) return redirect('/pick-tools')->with('status', 'You have subscribed to use all of my products! Please select which products below you would like to use.');
    }
    else
    {
        return view('membersHome')->with('productsWeCanUse', \App\ToolSelection::GetUsersProducts());
    }
}

一切都在当地工作正常,并在测试时也在生产但是在我的生产laravel日志中,我看到了这个错误.

Trying to get property of non-object {"exception":"[object] (ErrorException(code: 0): Trying to get property of non-object at MembersController.PHP:93)

93号线是

if(!Auth::user()->picked_tools)

当我正在应用auth中间件时,我无法理解pick_tools有时不会出现在Auth对象中,或者当时Auth对象不存在?

picked_tools是我的users表上的一列.它始终存在,脚本中没有与它的其他交互.

这可能会出错?再次,在本地和生产中测试我无法复制此错误,但我经常在生产日志中看到它.

解决方法:

您不应该在行动中定义中间件.使用构造函数.

However, it is more convenient to specify middleware within your
controller’s constructor.

(docs)

您必须按如下方式执行此操作

public function __construct()
{
    // this will only apply middleware to one action
    $this->middleware('auth')->only('indexPaid');
}

UPD.由于您定义的中间件不适用于正确的位置,因此您收到错误.访问操作时必须已定义它.所以使用routes文件或控制器构造函数.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐