TestModel::count(), 'active' => TestModel::where('status', 1)->count(), 'inactive' => TestModel::where('status', 0)->count(), 'deleted' => TestModel::onlyTrashed()->count(), ]; } /** * 获取状态分布统计 * * @return array */ public function getStatusDistribution(): array { return TestModel::select('status', DB::raw('count(*) as count')) ->groupBy('status') ->get() ->mapWithKeys(function ($item) { return [$item->status => $item->count]; }) ->toArray(); } /** * 获取每日创建数量统计 * * @param int $days * @return array */ public function getDailyCreationStatistics(int $days = 7): array { $startDate = now()->subDays($days)->startOfDay(); $endDate = now()->endOfDay(); return TestModel::select( DB::raw('DATE(created_at) as date'), DB::raw('count(*) as count') ) ->whereBetween('created_at', [$startDate, $endDate]) ->groupBy('date') ->orderBy('date') ->get() ->mapWithKeys(function ($item) { return [$item->date => $item->count]; }) ->toArray(); } }