我需要覆盖函数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()函数,一切都很好.
解决方法:
在最后一行,你调用parent :: toJson()方法,它在内部calls $this->jsonSerialize(),而这一个calls $this->toArray() – 你的重写函数,因此前进到无限的调用循环.
在重写的函数中,您应该调用parent :: toArray().
标签:php,laravel,api
来源: https://codeday.me/bug/20190827/1744828.html