gpt4 book ai didi

php - Laravel 中 Controller 查询 foreach 循环出现问题

转载 作者:行者123 更新时间:2023-11-29 18:12:49 25 4
gpt4 key购买 nike

大家好,

任何人都可以帮我解决这个代码吗?

public function submitranking(Request $req){
$dataCandidate = Candidate::all();

foreach($dataCandidate as $Candidate){
$judgeRate = Score::where('canId',$Candidate->id )
->where('catId',$req->catId )
->where('judgeId',$req->judgeId)
->sum('score');
dd($judgeRate);
}

}

当我dd($judgeRate)时,它应该显示所有考生的分数,但它只显示一条记录?我怎样才能让它循环显示所有考生的总分...请帮助

最佳答案

你应该尝试这个:

public function submitranking(Request $req){

$dataCandidate = Candidate::get();

foreach($dataCandidate as $Candidate){
$judgeRate = Score::where('canId',$Candidate->id )
->where('catId',$req->catId )
->where('judgeId',$req->judgeId)
->sum('score');

print('<pre style="color:red;">');
print_r($judgeRate);
print('</pre>');
}
exit;
}

或者你应该尝试这个:

public function submitranking(Request $req){

$dataCandidate = Candidate::get();

foreach($dataCandidate as $Candidate){
$judgeRate = Score::select('score_id',DB::raw("SUM(score) as score"))
->where('canId',$Candidate->id )
->where('catId',$req->catId )
->where('judgeId',$req->judgeId)
->get();

print('<pre style="color:red;">');
print_r($judgeRate);
print('</pre>');
}
exit;
}

关于php - Laravel 中 Controller 查询 foreach 循环出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47258097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com