gpt4 book ai didi

php - Laravel 无法理解为什么会尝试获取非对象的属性?

转载 作者:可可西里 更新时间:2023-10-31 23:01:06 27 4
gpt4 key购买 nike

我有一个 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 中间件时,我不明白为什么 picked_tools 有时不能在 Auth 对象中或者 Auth 对象当时不存在?

picked_tools 是我的用户表中的一列。它始终存在,脚本中没有其他与它的交互。

这里可能出了什么问题?同样,在本地和生产中测试我无法重现此错误,但我经常在生产日志中看到它。

最佳答案

你不应该在行动中定义中间件。为此使用构造函数。

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');
}

更新。由于您定义的中间件应用不在正确的位置,因此出现错误。当您访问操作时,它必须已经定义。所以使用路由文件或 Controller 构造函数。

关于php - Laravel 无法理解为什么会尝试获取非对象的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48238106/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com