【问题标题】:"Maximum execution time of 60 seconds is exceeded" when I override toArray() function当我覆盖 toArray() 函数时,“超过了 60 秒的最大执行时间”
【发布时间】:2019-08-28 11:28:30
【问题描述】:

我需要重写函数toArray() 以检查用户是否具有获取特定列的适当权限,所以我创建了这个函数:

public function toArray( $options = 0 )
{
    if(!auth()->user()->hasPermissionTo('users.show.email')) {
        $this->hidden[] = 'email';
    }

    // etc ... 

    return parent::toJson($options);
}

但是当我在控制器中使用 User::Get() 来获取所有用户列表时,我没有得到任何结果,但 60 秒后我得到:

[2019-04-06 23:18:33] local.ERROR: Maximum execution time of 60 seconds exceeded {"userId":1,"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalErrorException(code: 1): Maximum execution time of 60 seconds exceeded at C:\\xampp\\htdocs\\starter\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Arr.php:162)
[stacktrace]

如果我不重写 toArray() 函数,一切都很好。

【问题讨论】:

  • 你有递归问题吗?执行期间内存是否激增?

标签: php laravel api


【解决方案1】:

在最后一行,您调用了 parent::toJson() 方法,该方法在内部是 calls $this->jsonSerialize(),而这个 calls $this->toArray() — 您的重写函数,因此会进入无限循环。

在您被覆盖的函数中,您应该改为调用parent::toArray()

【讨论】:

  • 我在你回答 xD 前 5 秒才意识到这一点,但无论如何感谢你的回答。
猜你喜欢
  • 2020-04-29
  • 2020-03-22
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 2016-10-02
  • 2010-11-28
  • 1970-01-01
相关资源
最近更新 更多