猿问

使用 += 时 PHP foreach 不添加到变量

我从以下代码中得到了意想不到的结果:


$minutes = 0;

if($timeSpan === "game") {

  //stuff

} elseif($timeSpan === "season") {

  $games = Game::where('season_id', $timeSpanId)->where('team_id', $whoId)->where('stats_done', 'yes');

  foreach($games as $game) {

    $minutes += $game->periods * $game->period_length * 5;

  }

}


return $minutes;

这将返回 0,但我知道这不是我想要得到的,而且数据库在那里有数据,所以不应该是这种情况。


快速上下文:


$timeSpan 确实等于“季节”,经过测试,我知道这不是问题

此处的游戏查询确实返回结果。我知道是因为我取出 foreach 并用 $minutes = $games->count() 替换它并得到 5

有人知道这可能是什么吗?我觉得我以前在 foreach 循环中遇到过这个问题,所以我期待了解我对它们的了解在哪里。


谢谢!


蓝山帝景
浏览 190回答 1
1回答

一只名叫tom的猫

正如我所评论的,实际上您只是缺少get()将检索执行查询的结果。所以请执行如下$games = Game::where('season_id', $timeSpanId)->where('team_id', $whoId)->where('stats_done', 'yes')->get();
随时随地看视频慕课网APP
我要回答