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.
您必须按如下方式执行此操作
public function __construct()
{
// this will only apply middleware to one action
$this->middleware('auth')->only('indexPaid');
}
UPD.由于您定义的中间件不适用于正确的位置,因此您收到错误.访问操作时必须已定义它.所以使用routes文件或控制器构造函数.