whereYear('date', $now->year) ->whereMonth('date', $now->month) ->sum('amount'); // 本月分類統計(圓餅圖) $categoryStats = Expense::with('category') ->where('user_id', $userId) ->whereYear('date', $now->year) ->whereMonth('date', $now->month) ->get() ->groupBy(fn($e) => $e->category?->name ?? '未分類') ->map(fn($group, $name) => [ 'name' => $name, 'total' => $group->sum('amount'), 'color' => $group->first()->category?->color ?? '#6B7280', ]) ->values(); // 近6個月趨勢 $monthlyTrend = collect(range(5, 0))->map(function ($i) use ($userId, $now) { $month = $now->copy()->subMonths($i); $total = Expense::where('user_id', $userId) ->whereYear('date', $month->year) ->whereMonth('date', $month->month) ->sum('amount'); return [ 'month' => $month->format('Y/m'), 'total' => (float) $total, ]; }); return Inertia::render('Dashboard', [ 'monthlyTotal' => (float) $monthlyTotal, 'categoryStats' => $categoryStats, 'monthlyTrend' => $monthlyTrend, ]); } }