【发布时间】:2022-01-03 12:49:07
【问题描述】:
我写了一段代码,检索用户列表并在检索到的数据上查找进度,但是我需要根据进度对asc进行排序,应该是分页,但我不知道该怎么做
既然用户多,有没有办法不用foreach就可以按查询分页排序?
下面是代码,是找进度的代码,用foreach不好,有没有更好的办法??也需要分页
$users = \App\Models\ChallengeUserPivot::where('challenge_id', $challenge_id)->get();
foreach($users as $user){
$user_data = \App\Models\User::where('id', $user['user_id'])->first();
$temp_progress = [];
$temp_progress['total'] = 0;
$temp_progress['doing'] = 0;
foreach($challenge->ChallengeMissions as $key => $mission){
$temp = ChallengeFunction::missionState($user_data, $mission);
$temp_progress['total'] += (int)$temp['have_to_mission_count'];
$temp_progress['doing'] += $temp['cert_count'];
}
$user['progress'] = ($temp_progress['doing'] / $temp_progress['total']) * 100;
}
【问题讨论】:
-
到目前为止您尝试过什么?你被困在哪里了?您要对哪些部分进行分页? “不使用 foreach”是什么意思?
标签: php laravel sorting pagination