select('username', Db::raw('SUM(amount) as total_points')) // 计算每个用户名的积分总数 ->groupBy('username') // 按用户名分组 ->get(); foreach ($result as $v) { $user = User::where('username', $v->username)->first(); $user->money += $v->total_points; $user->save(); break; } } }