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文件或控制器构造函数.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...