为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
蓝山帝景 2021-12-03 19:41:59
我从以下代码中得到了意想不到的结果:$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 循环中遇到过这个问题,所以我期待了解我对它们的了解在哪里。谢谢!
查看完整描述

1 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超3个赞

正如我所评论的,实际上您只是缺少get()将检索执行查询的结果。所以请执行如下

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


查看完整回答
反对 回复 2021-12-03
  • 1 回答
  • 0 关注
  • 189 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信