count(); // 男宝宝数 (sex = 1) $maleCount = AppsBabyhealthBabys::where('delete_time', null)->where('sex', 1)->count(); // 女宝宝数 (sex = 2) $femaleCount = AppsBabyhealthBabys::where('delete_time', null)->where('sex', 2)->count(); // 未知性别 (sex = 0) $unknownCount = AppsBabyhealthBabys::where('delete_time', null)->where('sex', 0)->count(); return json([ 'code' => 200, 'msg' => '统计成功', 'data' => [ 'total' => $total, 'male' => $maleCount, 'female' => $femaleCount, 'unknown' => $unknownCount ] ]); } catch (DbException $e) { return json([ 'code' => 500, 'msg' => '统计失败:' . $e->getMessage() ]); } } /** * 统计用户数量 * @return Json */ public function getUserCounts() { try { // 总数 $total = AppsBabyhealthUsers::where('delete_time', null)->count(); // 父亲 (sex = 1) $fatherCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 1)->count(); // 母亲宝数 (sex = 2) $motherCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 2)->count(); // 未知性别 (sex = 0) $unknownCount = AppsBabyhealthUsers::where('delete_time', null)->where('sex', 0)->count(); return json([ 'code' => 200, 'msg' => '统计成功', 'data' => [ 'total' => $total, 'father' => $fatherCount, 'mother' => $motherCount, 'unknown' => $unknownCount ] ]); } catch (DbException $e) { return json([ 'code' => 500, 'msg' => '统计失败:' . $e->getMessage() ]); } } }