map(fn (string $platform) => [ 'platform' => $platform, 'latest' => SocialStat::where('platform', $platform) ->orderByDesc('recorded_date') ->first(), 'history' => SocialStat::where('platform', $platform) ->orderByDesc('recorded_date') ->limit(7) ->get(), ]); // Posting frequency - count by platform per month $postingFrequency = ContentBrief::where('status', 'posted') ->selectRaw("platform, strftime('%Y-%m', posted_date) as month, count(*) as count") ->whereNotNull('posted_date') ->groupBy('platform', 'month') ->orderBy('month') ->limit(30) ->get(); return view('social-hub.index', compact('stats', 'postingFrequency')); } public function storeStats(Request $request): RedirectResponse { $validated = $request->validate([ 'platform' => 'required|in:instagram,tiktok,youtube', 'handle' => 'nullable|string|max:100', 'followers' => 'required|integer|min:0', 'following' => 'nullable|integer|min:0', 'posts_count' => 'nullable|integer|min:0', 'engagement_rate' => 'nullable|numeric|min:0|max:100', 'avg_views' => 'nullable|integer|min:0', 'avg_likes' => 'nullable|integer|min:0', 'avg_comments' => 'nullable|integer|min:0', ]); $validated['recorded_date'] = today()->toDateString(); SocialStat::create($validated); return back()->with('success', '📊 Stats updated!'); } }