【发布时间】:2014-12-28 18:17:57
【问题描述】:
我正在使用宅基地。我有这个代码
$researches = Auth::user()->load(['researches' => function ($q)
{
$q->orderBy('situational', 'desc');
$q->orderBy('id', 'asc');
$q->with('research_type');
}])->researches;
$researches = FormatController::sort($researches);
dd($researches);
当我运行此程序时,页面无休止地加载,并以“网关 - 超时”结束,我必须重新启动宅基地,因为没有任何效果了。
在 FormatController 中运行排序方法后崩溃。它看起来像这样:
public static function sort($values, $key = 'id')
{
$sorted = [];
foreach ($values as $v)
{
$sorted[$v->$key] = $v;
}
return $sorted;
}
我不知道它为什么会崩溃。这个方法在这部分代码之前用过几次,效果很好。
如果我在 return 语句之前进行 die-dump,我会得到我想要的数组
有什么想法吗?
【问题讨论】:
-
当您说崩溃时,您的意思是 Web 服务器本身崩溃(即 Apache segfault)还是 PHP 产生了致命错误?
-
好吧,我不确定。它加载了很长一段时间,给了我一个“网关 - 超时”错误,然后我尝试加载的每个页面都做同样的事情(尽管它们在我从上面运行脚本之前工作)。
-
$sorted[$v->$key] = $v;你在这里做什么?
-
我使用研究的“id”属性作为数组的键。这样我就可以访问我的研究,例如“$researches[$anyResearchId]”。如果我不这样做,键就会从 0 开始并向上计数。因此,我无法访问特定研究
-
如果我不使用
Auth::user()->load而是使用User::with它可以工作。我不知道为什么,因为Auth:user()只是用户对象,就像我用User::find(id)得到的对象一样